Jump to Section
Getting Started: Setting Up Your Project#
Before you begin, ensure your web project includes Microsoft.AspNet.Web.Optimization package.
If it’s missing, install it using NuGet:
Once installed, you’re ready to set up your bundles.
Step 1: Create the BundleConfig#
In the App_Start folder, create a BundleConfig.cs file if it doesn’t already exist.
This file serves as the central hub for managing your bundles.

BundleConfig file in ASP .NET project structure
Step 2: Design Your Bundles#
Group your CSS and JavaScript files into logical bundles.
For example:
- Home Page Scripts: Include libraries and custom scripts for the homepage.
- Global Styles: Combine all global stylesheets into a single bundle.
Here’s an example setup:
💡Why Bundling Works: Bundling reduces HTTP requests, while minification shrinks file sizes, speeding up page loads.
Step 3: Enable Optimization for BundleTable#
To activate bundling and minification, disable debug mode in your Web.config file:
This ensures the bundling engine optimizes your scripts and styles.
Step 4: Implement Bundles in Razor Views#
Use the @Scripts.Render and @Styles.Render helpers in your Razor views to include the bundles.
Add the required namespace in ~/Views/Web.config:
In your Razor views, reference the bundles like this:
Step 5: Fix Browser Caching Issues#
Have users reported outdated CSS or JavaScript after updates?
Browser caching might be the issue.
Fix this by appending unique version tokens to your bundle URLs.
Here’s a C# extension method to add a "last modified" token automatically:
This great C# extension was found on StackOverflow.
All credit goes to author Ranjeet patil (thanks)!
Update your BundleConfig.cs file to use this extension:
This ensures every build generates a unique version, forcing browsers to load the latest files.
You will get a unique extra parameter in your script URL:
Step 6: Initialize Bundling in Global.asax#
Finally, initialize bundling during application startup in Global.asax:
Celebrating Your Optimized HTML Output#
After completing the bundling and minification process, your HTML will look like this:
Before Bundling:
After Bundling and Minification:
Key Benefits:
- Reduced Requests: Bundling combines multiple files into one for both CSS and JS.
- Cache Busting: The ?v=unique-version ensures browsers fetch the latest files whenever changes occur.
- Simplified Code: Cleaner and more maintainable HTML.
Conclusion: Fast, Efficient, and Ready for Action#
Following these steps, your ASP.NET website is optimized for speed and efficiency.
Reasonable bundling and minification will reduce load times and enhance user experience.
What’s Next?
Explore our blog for advanced ASP.NET and Umbraco CMS techniques.