vince anon
vince anon

Reputation: 73

MVC 4 bundle for jquery using {version} not rendering

This is driving me crazy. I have a bundle

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

and I have in the Scripts folder

and in the layout page have the following:

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryui")
    @Scripts.Render("~/bundles/jqueryval")

when I run in debug or release build it does not pickup jquery. I stepped into the code from the debugger and when the bundle is created for jquery it does not contain anything. Does anyone know why the {version} wild card is not picking up jquery? Any help greatly appreciated.

Upvotes: 4

Views: 4151

Answers (3)

gilm0079
gilm0079

Reputation: 605

I had this exact same issue and found the fix. I noticed that if I used the jquery file's version number instead of the {version} token that it worked fine to use in bundling/minification. The issue was that it was not replacing the {version} token and then failed to pull the jquery file into the bundle.

It looks like the System.Web.Optimization dll is responsible for converting the {version} token in the bundle config. I ended up using NuGet to update this dll. I also just updated all of the MVC related dlls while I was at it (Razor, MVC, web pages, etc). After that it worked.

You can see if this is your problem by splitting up the operations for building and adding bundles to the bundle config. New up a bundle object. put a break point on it. Perform the Include method then perform the add method on the bundle config to add the new bundle. When you step into the bundle you can look inside of it after it performs the Include method to see if the Item property increments accordingly. It was not incrementing up for me when it was a problem.

Also, here is a very well done reference for migrating to MVC 5 from MVC 4. I had some work to do to get all of my assembly references updated after I updated the NuGet packages.

http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2

ASP.NET: This method cannot be called during the application's pre-start initialization stage

Upvotes: 0

tjp69
tjp69

Reputation: 1088

What fixed the problem for me was moving the @Scripts.Render("~/bundles/jquery") statement from the bottom of the _Layout.cshtml to the top. By default, Microsoft put this render statement below the footer. I moved it into the section along with the @Scripts.Render("~/bundles/modernizr") and then the script in my index.cshtml started working.

So apparently the issue was that when my script inside index.cshtml tried to execute, JQuery was not yet loaded because it was at the bottom of the page.

Upvotes: 5

Rutger
Rutger

Reputation: 321

In the Global.asax are you loading your bundle?

For example:

public class MvcApplication : System.Web.HttpApplication
{
        protected void Application_Start()
        {
            ....

            // Register Bundles
            BundleConfig.RegisterBundles(BundleTable.Bundles);

        }
}

Upvotes: 0

Related Questions