Usage of BundleConfig.cs file with sample an example in MVC

Follow below steps:

Step-1:

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

Install-Package Microsoft.AspNet.Web.Optimization

Step-2:

Then under the App_Start folder create a new cs file called BundleConfig.cs.

Add below code in Bundleconfig file;

namespace BundleConfigEx
{
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle(“~/bundles/sciptsJS”).Include(“~/Scripts/jquery-{version}.js”));
BundleTable.EnableOptimizations = true;
}
}

}

bundles.Add(new ScriptBundle(“~/bundles/sciptsJS”).Include(“~/Scripts/jquery-{version}.js”));

we will add number of bundles(Based on Our requirement) like above line of code. 

Step-3:

For this we will register our bundles in Global.asax.cs file like below;

BundleConfig.RegisterBundles(BundleTable.Bundles);

Step-4:

Now we can call this bundle in our _layout file (or) View(Index.cshtml) like below

@Scripts.Render(“~/bundles/sciptsJS”)

Leave a Reply

Your email address will not be published. Required fields are marked *