Search code examples
asp.net-mvcbundling-and-minificationasp.net-optimization

Bundled scripts/css are not being recognized


I am very new to Bundling and minification, I try to implement it for the first time in my MVC project.

I have added a BundleConfig.cs file:

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        //libs scripts
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
        "~/Scripts/libs/jquery/jquery-{version}.js",
        "~/Scripts/libs/jquery/jquery-ui-{version}.js",
        "~/Scripts/libs/jquery/jquery.mask*",
        "~/Scripts/libs/jquery/jquery.validate*"));

        bundles.Add(new ScriptBundle("~/bundles/ko").Include(
        "~/Scripts/libs/ko/knockout-{version}.js"));

        //site scripts
        bundles.Add(new ScriptBundle("~/bundles/site").Include(
                    "~/Scripts/site/*.js"));

        bundles.Add(new StyleBundle("~/Content/site/").Include("~/Content/site/*.css"));
    }
}

And added in Global.asax:

BundleConfig.RegisterBundles(BundleTable.Bundles);

Then I rendered the scripts/css in my Layout page:

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/site/Fonts.css")
    @Styles.Render("~/Content/site/Site.css")
    @RenderSection("styles", required: false)

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/ko")
    @Scripts.Render("~/bundles/site")
    @RenderSection("scripts", required: false)
</head>

But this is not working, I keep getting all kind of errors that indicate that the scripts and css are not being recognized.

For example:

Uncaught ReferenceError: jQuery is not defined

What am I doing wrong?


Solution

  • As per @BasantaMatia comment that gave me the idea, setting:

    BundleTable.EnableOptimizations = true
    

    In the Global.asax file, after:

    BundleConfig.RegisterBundles(BundleTable.Bundles);
    

    Solved the issue.