Search code examples
c#optimizationasp.net-mvc-5stylesasp.net-bundling

C# MVC - Styles not rendering when optimizations are set to true


I have the following code in Config.cs:

        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-2.2.3.min.js",
                    "~/Scripts/jquery.validate.min.js",
                    "~/Scripts/jquery.validate.unobtrusive.min.js",
                    "~/Scripts/jquery.unobtrusive-ajax.min.js"
                    ));
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));
        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/respond.js"
                  ));
        bundles.Add(new StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/font-awesome.min.css",
                  "~/Content/css/site.css", 
                  "~/Content/css/ab-style.css"));

The styles are not rendering correctly and page is looking odd. But everything works fine when I set BundleTable.EnableOptimizations = false in config.cs. Does anybody have a clue as to why this is happening.

NOTE: There is no Debug= "true" in Web.config and there is no conflict of virtual path as there is no "~/bundles/jquery", "~/bundles/bootstrap" ... folders in my project.


Solution

  • Create your bundle in virtual folder.

      bundles.Add(new StyleBundle("~/Content/css/AllMyCss.css").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/font-awesome.min.css",
                  "~/Content/css/site.css", 
                  "~/Content/css/ab-style.css"));
    

    and use the following code to render a bundle

    @Styles.Render("~/content/css/AllMyCss.css")