How to bundle resource files in C# and ASP.NET

Written by
Published on

Many ASP.NET developers don't realize that the .NET framework comes with a built in bundler for all of their many resource files. What is a bundler you may ask? Well, a bundler is a program that can make multiple files, in this case CSS and JavaScript files, and can convert into a single file.

This can save you space as the files that you load over a network will be smaller in size and it can load a website faster since you will only need to download 1 file instead of multiple. Most browsers tend to have limit on the number of files that can be downloaded in a single load.

The first thing that you'll want to do is to include the right namespace directives for the job. In this case, the bundle classes can be found in the System.Web.Optimization namespace:

using System.Web.Optimization;

Next up:

Create the Bundle in BundleConfig.cs

If you check your App_Start folder in your project you should see the BundleConfig.cs file already there with some default content in place already.

That should look something like the following:

  // Order is very important for these files to work, they have explicit dependencies
bundles.Add(new ScriptBundle("~/bundles/MsAjaxJs").Include(
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjax.js",
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjaxApplicationServices.js",
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjaxTimer.js",
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjaxWebForms.js"));

Make note of the comment that order is important. So be sure to take that into account when creating a resource bundle.

ASP.NET creates default bundles for you for internal JavaScript scripts. In this article, you will create your own bundles.

In the BundleConfig.cs file you will have the following function in which the bundles are created.

public static void RegisterBundles(BundleCollection bundles)

The following will add a new bundle into the collections by passing in a new StyleBundle object with the relative name that you wish to use.

bundles.Add(new StyleBundle("~/bundles/mastercss").Include(
                "~/styles/_master.css",
"~/styles/_about.css" ));

In this particular example, those two files would be added into their own unique bundle of text. Notice that the first argument to the StyleBundle constructor is the relative path, which can be any identifier that you wish. In this case I called the bundle mastercss. That's the name (path) that I'll be using later to retrieve the bundle in my code.

Retrieving the Bundle

With the Optimization namespace included, you can now access the BundleTable directly and retrieve any registered bundles in any part of your code.

var cssBundle = BundleTable.Bundles.GetBundleFor("~/bundles/mastercss");
var css = cssBundle.GenerateBundleResponse(new BundleContext(new HttpContextWrapper(HttpContext.Current), BundleTable.Bundles, string.Empty)).Content;

The GenerateBundleResponse method will return the bundle response in the form of a minified and bundled string.

Now that you have that string you can pretty much do what you'd like with it. One of the more popular options in this case, is to save the content into a file and to cache it for faster retrieval later on.

But you can also serve the bundle directly as well to the page through a Literal control.

Literal litCss = new Literal();
litCss.Text = "<style>" + css + "</style>";

You can then add this new Literal control onto your page through the Page.Controls collection if you'd wish.

Though I would not use the Bundler in this way, as each page load would end up generating a new file each and every time and thus, not be the most optimal performance wise.

A few final words:

There are certain cases in which the bundler will fail to perform from what I've seen so far. If your CSS is invalid in any way, the content will not get minified correctly, however it should still stich together all of the required files without any problem.

Leave a comment

No messages posted yet
Walter Guevara is a software engineer, startup founder and currently teaches programming for a coding bootcamp. He is currently building things that don't yet exist.

New articles published each week. Sign up for my newsletter and stay up to date.

In this collection

Dot Net Developers

Add a comment

Send me your weekly newsletter filled with awesome ideas
Post