Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add BundleConfig.cs to my project?

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.

like image 346
Maverick Avatar asked Feb 10 '14 02:02

Maverick


People also ask

What is BundleConfig Cs in MVC?

BundleConfig.cscs file present in a default MVC5 application. This file contains RegisterBundles() method which is being called at Application_Start() event by global. asax. cs file. This RegisterBundles() method contains all the bundles created in the application.

What is BundleConfig Cs in asp net?

Bundling is a new feature in ASP.NET 4.5 that makes it easy to combine or bundle multiple files into a single file. You can create CSS, JavaScript and other bundles. Fewer files means fewer HTTP requests and that can improve first page load performance.

Where do I put bundle config in asp net core?

Click on wwwroot folder, select the css file you want minified, then right click and choose bundler & minifier. Then from popup minify file. It will be the same file name with the minified version. Also generate bundleconfig.


1 Answers

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

like image 188
vmg Avatar answered Sep 22 '22 03:09

vmg