Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Bundling and minification in MVC3 and Asp.Net 4.0

Bundling and minification in MVC3 and Asp.Net 4.0

04 Apr 2024
Intermediate
6K Views
6 min read
Learn with an interactive course and practical hands-on labs

ASP.NET MVC with Web API Foundations Course - Free

Bundling and Minification: An Overview

You can also implement bundling and minification techniques within Asp.net MVC3 and Asp.net 4.0. In the previous article Asp.net MVC 4 performance optimization with bundling and minification, I have explained both techniques, Now I would like to share with you could achieve this functionality with .Net Framework 4.0. So In this MVC Tutorial, we will explore more about the Bundling and minification in MVC3 and Asp.Net 4.0. Consider our ASP.NET MVC Course for a better understanding of all MVC core concepts.

How to do it in MVC3 and Asp.Net 4.0

Adding References

First of all, add the references of System.Web.Optimization.dll and WebGrease.dll to your MVC3 and Asp.Net 4.0 projects as shown below. You can download the DLL by using the download link.

How to do it in MVC3 and Asp.Net 4.0

Creating Bundle

Now create the bundle for your CSS and js files within the Global.asax file as shown below.

public static void RegisterBundles(BundleCollection bundles)
{
 //Creating bundle for your css files
 bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/mystyle.min.css", 
 "~/Content/site.min.css"));
 //Creating bundle for your js files
 bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
 "~/Scripts/jquery-1.5.1.min.js",
 "~/Scripts/jquery.validate.min.js",
 "~/Scripts/jquery.validate.unobtrusive.min.js"));
}

Here, I have created the bundle of all required CSS and JS files. You can also add your own CSS and js files with complete paths using the Include method.

Registering Bundle

You need to register the above-created bundles within the Application_Start event of Global.asax like as

protected void Application_Start()
{
 RegisterBundles(BundleTable.Bundles);
 // Other Code is removed for clarity
}

Adding Bundles to Layout Page in MVC3

Now you can add the above-created style and script bundles to the Layout page or where you want to use them as shown below:

@System.Web.Optimization.Styles.Render("~/Content/css")
@System.Web.Optimization.Scripts.Render("~/bundles/jquery")

Adding Bundles to Master Page in Asp.Net 4.0

Now you can add the above-created style and script bundles to the Master page or where you want to use them as shown below:

<%: Styles.Render("~/Content/css") %>
<%: Scripts.Render("~/bundles/jquery")%>

In Asp.Net 4.0 you are also required to add System.Web.Optimization namespace and assembly Microsoft.AspNet.Web.Optimization.WebForms reference to the web.config file of your Asp.Net 4.0 project as shown below:

<system.web>
 <compilation debug="true" targetFramework="4.0" />
 <pages>
 <namespaces>
 <add namespace="System.Web.Optimization" />
 </namespaces>
 <controls>
 <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
 </controls>
 </pages>
<!-- Other Code is removed for clarity -->
</sytem.web >

You need not to make any changes in web.config file of your MVC3 project.

Enabling Bundling and Minification in debug mode

Bundling and minification don't work in debug mode. So to enable these features you need to add the below line of code within in Application_Start event of Global.asax.

protected void Application_Start()
{
 BundleConfig.RegisterBundles(BundleTable.Bundles);
 //Enabling Bundling and Minification
 BundleTable.EnableOptimizations = true; 
 // Other Code is removed for clarity
}

How it works.

Now run your application and you will see that all the CSS and js files are converted to single css and js file as shown below:

Minification

Minification is a technique for removing unnecessary characters (like white space, newline, tab) and comments from the JavaScript and CSS files to reduce the size which causes improved load times of a webpage. There are so many tools for minifying the JS and CSS files. JSMin and YUI Compressor are the two most popular tools for minifying the JS and CSS files. Use these tools for minifying your CSS and JS files and use them in your application with the ".min" suffix. So that you can easily identify that this is a minimized version of your CSS or JS file.

Conclusion

So in this article, we have learned about Bundling and minification in MVC3 and Asp.Net 4.0. I hope you enjoyed learning these concepts while programming with Asp.Net. Feel free to ask any questions from your side. Your valuable feedback or comments about this article are always welcome. Level up your career in MVC with our ASP.Net Core Certification.

FAQs

Bundling is one of the features of MVC. By implementing this, we can improve performance request load time. Minification is the process of removing unnecessary data without changing its functionality such as removing white spaces, comments, converting the large variable names to small, etc

To improve performance by reducing the number of server requests and reducing the size of the requested static assets.

It is a file containing the MVC application's default bundles.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this