Search code examples
asp.netbundling-and-minification

Bundled scripts not rendering properly


I'm getting my feet wet with ASP.NET MVC and I've hit a snag with bundling.

Here are the contents of my BundleConfig.cs

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
    "~/bower_components/jquery/dist/jquery.js"
));

bundles.Add(new ScriptBundle("~/bundles/jquery-ui").Include(
    "~/bower_components/jquery-ui/ui/core.js",
    "~/bower_components/jquery-ui/ui/widget.js",
    "~/bower_components/jquery-ui/ui/position.js",
    "~/bower_components/jquery-ui/ui/menu.js",
    "~/bower_components/jquery-ui/ui/autocomplete.js"
));

bundles.Add(new ScriptBundle("~/bundles/mondernizr").Include(
    "~/Scripts/modernizr-*"
));

bundles.Add(new StyleBundle("~/Content/css").Include(
    "~/bower_components/bootstrap/dist/css/bootstrap.css",
    "~/Content/PagesList.css",
    "~/Content/Site.css",
    "~/bower_components/font-awesome/css/font-awesome.css"
));

And here is the area in my _Layout html where I want to render the scripts and css in the head.

@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/jquery", "~/bundles/jquery-ui")
@Scripts.Render("~/bundles/modernizr")

As far as I'm aware I just need to use the same relative path as I specified in the bundles to get it to output either the full list of script/link tags or the bundled version. Instead I get the following without any of the versioning strings at the end that I was expecting.

<link href="/Content/css" rel="stylesheet"/>
<script src="/bundles/jquery"></script>
<script src="/bundles/jquery-ui"></script>
<script src="/bundles/modernizr"></script>

This happens regardless if I have BundleTable.EnableOptimizations set to true or false.


Solution

  • I was following a tutorial for using bower and bundling here, as well as the tutorial from microsoft, here. What wasn't explained in either tutorial was that the class has to be called in the Application_Start in the Global.asax.cs file with the following:

    BundleConfig.RegisterBundles(BundleTable.Bundles);
    

    Also I had misspelled modernizr in my configuration as mondernizr.