Using BundleConfig on Asp.net MVC 5
Hi everyone,
I’m going to tell how we can use BundleConfig class for bundling on MVC 5 project. We can optimize our js and css files by using BundleConfig class.
Bundling is used to improve request load time. It reduces the number of requests to the server and reduces the size of requested assets (such as CSS and JavaScript.)
At first, we can create a new empty MVC project on Visual Studio.
Then we will install the optimization package in that way,
Tools > NugetPackageManager > Package Manager Console > Install-Package Microsoft.AspNet.Web.Optimization
Under the AppStart, we will add BundleConfig.cs class. I added my .js and .css inside the BundleConfig method.
Now, we created the BundleConfig class but we have to say to MVC “Hey! we have bundling and now it’s your turn. Please let’s add the System.Web.Optimization as a namespace in Global.asax”
Because Global.asax should see the App_Start which contains ‘BundleConfig.RegisterBundles(BundleTable.Bundles);’
We have already set routing inside the BundleConfig class. By the way, when you’re setting routing for .css , you should use StyleBundle method, if you’re setting routing for .js, you should use ScriptBundle method.
Finally, you should add your css file like @Styles.Render(“~/bundle/styles/..”) instead of css file in your .cshtml (inside the head tag)
Then, you should add your js file like @Scripts.Render(“~/bundles/scripts/..”) instead of js file in your .cshtml (last before the </body> tag)
Thank you!
That was my first story. I hope you like it!