Why are my style bundles not rendering correctly in ASP.NET MVC 4?

前端 未结 4 1202
天命终不由人
天命终不由人 2021-01-31 08:04

So I am new to ASP.NET MVC 4 (well, I used 3 a little).

Anyway, in my BundleConfig.cs file, I am trying to load the Twitter Bootstrap css files and an additional s

相关标签:
4条回答
  • 2021-01-31 08:33

    I don't use bundles for Bootstrap (as an exception), but its minified versions, so this works for me:

    <!DOCTYPE html>
    <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
    <!--[if IE 7]>    <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
    <!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
    <!--[if gt IE 8]><!--><html class="no-js"><!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>@ViewBag.Title</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width">
        <link rel="stylesheet" href="@Url.Content("~/Content/bootstrap.min.css")">
        <style>
            body {
                padding-top: 60px;
                padding-bottom: 40px;
            }
        </style>
        <link rel="stylesheet" href="@Url.Content("~/Content/bootstrap-responsive.min.css")">
        @Styles.Render("~/Content/less")
        @Scripts.Render("~/bundles/modernizr")
    </head>
    <body>
    

    The part with the inline style is from http://www.initializr.com/ for the top NavBar, if you don't need it, just remove.

    Alternatively, you can do the same in your styles.css file:

    @import url('bootstrap.min.css');
    
    body {
        padding-top: 60px;
        padding-bottom: 40px;
    }
    
    @import url('bootstrap-responsive.min.css');
    
    You CSS Code below
    
    0 讨论(0)
  • 2021-01-31 08:45

    As paul correctly stated, bundles as set to automatically ignore 'min' files. But ignoring this rule all together is a bad practice, please refer to this answer Bundler not including .min files to a better solution (check both first and second answers).

    0 讨论(0)
  • 2021-01-31 08:50

    Just to let anybody else know who cant find a working solution easily in this long thread, I had the exact same issue and i solved it in the same way as suggested in edits found at the bottom of the actual question!

    I started a new project MVC4 Web Application, created routes for bootstrap bundles in the exact same code shown above:

    bundles.Add(new StyleBundle("~/Content/css").Include(
                "~/Content/bootstrap.min.css",
                "~/Content/bootstrap-responsive.min.css",
                "~/Content/site.css"));
    

    But due to unknown reasons, it was not loading.. Finally i gave up and decided to google it and found this page.

    I tried adding the following as the first line in BundleConfig.cs file. Recompiled and everything started working and now the styles were correctly being loaded now.

    bundles.IgnoreList.Clear();
    

    Maybe this helps somebody.

    0 讨论(0)
  • 2021-01-31 08:55

    The default behaviour is for the IgnoreList to ignore any minified scripts.

    You can either remove the '.min' from their names, or clear the IgnoreList.

    0 讨论(0)
提交回复
热议问题