4

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

  • jquery-1.9.1.js
  • jquery-1.9.1.min.js
  • jquery-1.9.1.min.map

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.

3
  • is it rendering any scripts at all? If you open up the page source once the page is loaded are there any scripts being loaded? Commented Jun 7, 2013 at 15:25
  • Not for Jquery for others its working but only if I don't use the {version} wild card. It seems the {version} wildcard needs something. The project was original in VS 2010 and then we moved over to VS 2012 but we were never using the {version} attribute in the bundles. Commented Jun 7, 2013 at 15:56
  • Totally bizarre. Looks identical to my code. So not sure i am afraid! Commented Jun 7, 2013 at 20:11

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

it should work in that way MS have added it. can you please show the script you written on index?
Confimed when trying to add a datepicker to a partial view. Moving the scripts to top fixed the issue (I kept the RenderSection("scripts") at the bottom though).
This was also my solution. Please accept this as correct!
0

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);

        }
}

1 Comment

Yep its there protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.