Matt Dawdy
Matt Dawdy

Reputation: 19717

ASP.Net MVC 4 Bundles

Lots of code that I have seen reference this:

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Which is great, and it works...if "something" is included. Do I have to add a reference to get these? Use NuGet? Copy a DLL? Where does this come from?

When I run my project, I get a 404 for that resource.

Upvotes: 9

Views: 13021

Answers (5)

francorobles
francorobles

Reputation: 41

  1. Go to App_Start folder.
  2. Opoen BundleConfig.cs
  3. Inside the RegisterBundles method, check if you have the following.

bundles.Add(new ScriptBundle("~bundles/jqueryval").Include("~/Scripts/jquery.min.js"));

Please not that inside the Include method, it is including for the files in your project.

Upvotes: 0

ProVega
ProVega

Reputation: 5914

FYI - I have seen many examples of using Bundles in MVC, but most neglect to mention that the assemblies for this are in System.Web.Optimization.dll and you can get this from NuGet by adding the Microsoft ASP.NET Web Optimization Framework package.

Upvotes: 9

Martin Liversage
Martin Liversage

Reputation: 106826

You need to create the bundle. This is often done in the App_Start\BundleConfig.cs file in your ASP.NET MVC 4 project. It is all explained in Bundling and Minification .

In the BundleConfig class you need something like this (this method should execute in Application_Start):

public static void RegisterBundles(BundleCollection bundles) {
  bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
              "~/Scripts/jquery-{version}.js"));

  bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
              "~/Scripts/jquery.unobtrusive*",
              "~/Scripts/jquery.validate*"));

  // ... more registrations ...
}

The javascript source files should exists in the Scripts folder. The tutorial linked above explains how minified versions are bundled in the release build etc.

Upvotes: 11

Iswanto San
Iswanto San

Reputation: 18569

Yes, you must register the bundles in your application.

Global.asax.cs :

      protected void Application_Start() {

        AreaRegistration.RegisterAllAreas();

            // Register the bundles
            BundleConfig.RegisterBundles(BundleTable.Bundles);
      }

BundleConfig.cs :

     public class BundleConfig
     {
        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*",
                        "~/Scripts/jquery.livequery.js",
                        "~/Scripts/jquery.numeric.js"
                       ));               
        }

So when you put this code in your view :

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

It will render 4 javascript files.

More : Bundling and Minification

Upvotes: 7

Dimitar Dimitrov
Dimitar Dimitrov

Reputation: 15148

In your project App_Start/BundleConfig.cs is the declaration for all bundles. Consider this:

    bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
        "~/Scripts/jquery.unobtrusive*",
        "~/Scripts/jquery.validate*"));

In this case if you reference "~/bundles/jqueryval" it will include the 2 listed scripts for you, and as a bonus, it will minify them (if you run your project in "Release" mode).

Take a look at this for more details.

Upvotes: 0

Related Questions