103

I have an ASP.Net MVC project and I want to implement bundling, but everything I can find on the internet directs me to open BundleConfig.cs in App_Start - however this file does not exist in my project. I have only three files in that folder: FilterConfig, RouteConfig and WebApiConfig.

Bundle config wasn't generated when I created the solution (IIRC it was a blank ASP.NET MVC project at the beginning).

It seems like this should be really easy to do, but I just plain cannot figure it out.

P.S. Just to clarify to those not reading closely, this is for a MVC4/.Net 4.5 app created from scratch. The solution is marked below.

4
  • You can't find it because this is only included in ASP.NET 4.5 project templates. I assume you are using an earlier version of ASP.NET. Commented Feb 10, 2014 at 2:50
  • Possible duplicate of How to add reference to System.Web.Optimization for MVC-3-converted-to-4 app
    – Liam
    Commented Oct 20, 2015 at 10:45
  • 2
    @Liam Nope. As it clearly states in the question, this is regarding an app created anew, not converted from MVC3. It also pretty clearly is for how to add the BundleConfig.cs file - not how to add a reference to System.Web.Optimization (something that is totally unnecessary in this question's scenario). I have to assume you were trying to comment on an altogether different question.
    – Maverick
    Commented Oct 21, 2015 at 5:10
  • @Liam - actually, I see perhaps you may have been confused by the answer, which includes the step "add the Microsoft.AspNet.Web.Optimization nuget package to your web project" which was not part of the solution to the above problem. What was the solution was adding the BundleConfig.cs file. I did say this in a comment on the answer...but that comment (and all others on the answer) seem to have disappeared.
    – Maverick
    Commented Oct 21, 2015 at 5:26

2 Answers 2

179

BundleConfig is nothing more than bundle configuration moved to separate file. It used to be part of app startup code (filters, bundles, routes used to be configured in one class)

To add this file, first you need to add the Microsoft.AspNet.Web.Optimization nuget package to your web project:

Install-Package Microsoft.AspNet.Web.Optimization

Then under the App_Start folder create a new cs file called BundleConfig.cs. Here is what I have in my mine (ASP.NET MVC 5, but it should work with MVC 4):

using System.Web;
using System.Web.Optimization;

namespace CodeRepository.Web
{
    public class BundleConfig
    {
        // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
        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.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                      "~/Scripts/bootstrap.js",
                      "~/Scripts/respond.js"));

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

Then modify your Global.asax and add a call to RegisterBundles() in Application_Start():

using System.Web.Optimization;

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

A closely related question: How to add reference to System.Web.Optimization for MVC-3-converted-to-4 app

1
  • 2
    nuget should add a boilerplate version of this.
    – niico
    Commented Jun 18, 2016 at 22:40
1

If you are using "MVC 5" you may not see the file, and you should follow these steps: http://www.techjunkieblog.com/2015/05/aspnet-mvc-empty-project-adding.html

If you are using "ASP.NET 5" it has stopped using "bundling and minification" instead was replaced by gulp, bower, and npm. More information see https://jeffreyfritz.com/2015/05/where-did-my-asp-net-bundles-go-in-asp-net-5/

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.