Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ASP.NET 4.5 Bundling & a CDN (eg. CloudFront)

ASP.NET 4.5 has a great new bundling feature and appears to have some support for use of CDNs. The example given by Microsoft for use of the bundling feature with a CDN is this

public static void RegisterBundles(BundleCollection bundles) {   //bundles.Add(new ScriptBundle("~/bundles/jquery").Include(   //            "~/Scripts/jquery-{version}.js"));    bundles.UseCdn = true;   //enable CDN support    //add link to jquery on the CDN   var jqueryCdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js";    bundles.Add(new ScriptBundle("~/bundles/jquery",             jqueryCdnPath).Include(             "~/Scripts/jquery-{version}.js"));    // Code removed for clarity. }  

Which seems to suggest that you need tell it explicitly the path to your file on the CDN.

The CloudFront CDN (and I presume many others) gives you a subdomain which mirrors your own. When you hit http://uniquesubdomain.cloudfront.net/js/myfile.js?v=1 it serves up http://mydomain.com/js/myfile.js?v=1

This way you can simply prefix all your links with http://uniquesubdomain.cloudfront.net/ and your files are server from CloudFront.

Is the ASP.NET 4.5 bundling feature compatible with this type of CDN? Is there a built-in way to have the bundling feature prefix all its links with your CDN domain?

Eg.

bundles.UseCdn = true; var myBundle= new ScriptBundle("~/bundles/js", "https://uniquedomain.cloudfront.net/"); myBundle.Include("~/js/file1.js"); myBundle.Include("~/js/file2.js"); 

would cause

    <script src="https://uniquedomain.cloudfront.net/bundles/js?v=6y-qVPSK3RYOYHfPhOBDd92H4LjEjs-D3Hh2Yml6CXA1"></script> 
like image 615
Mr. Flibble Avatar asked Nov 14 '12 09:11

Mr. Flibble


People also ask

Can we use bundling and minification with ASP NET web forms like MVC?

To optimize the performance of an application I found that bundling and minification can significantly improve the performance. It can be applied on MVC as well as in ASP.NET web forms.

What is bundling in asp net core?

Bundling combines multiple files into a single file. Bundling reduces the number of server requests that are necessary to render a web asset, such as a web page. You can create any number of individual bundles specifically for CSS, JavaScript, etc.

What is difference between bundling and minification?

Both bundling and minification are the two separate techniques to reduce the load time. The bundling reduces the number of requests to the Server, while the minification reduces the size of the requested assets.

How do you use bundling and minification in MVC 5?

Bundling and minification can be enabled or disabled in two ways: either setting the value of the debug attribute in the compilation Element in the Web. config file or setting the enableOptimizations property on the BundleTable class. In the following example, debug is set to true in web.

What is ASP bundling in ASP NET?

ASP.NET 4.5 Developer Preview introduced bundling, which combines multiple JavaScript files for faster loading with less number of requests for download and minification, which reduces the size of JavaScript and CSS files by removing unneeded characters . Combination of these bundling and minification helps web pages to load very faster.

What is bundle and minification in ASP NET?

by Rick Anderson. Bundling and minification are two techniques you can use in ASP.NET 4.5 to improve request load time. Bundling and minification improves load time by reducing the number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript.)

What is less bundling in MVC?

LESS, CoffeeScript, SCSS, Sass Bundling. The bundling and minification framework provides a mechanism to process intermediate languages such as SCSS, Sass, LESS or Coffeescript, and apply transforms such as minification to the resulting bundle. For example, to add .less files to your MVC 4 project: Create a folder for your LESS content.

What is jQuery bundling in MVC 4?

For ASP.NET MVC 4, this means with a debug configuration, the file jquery-1.7.1.js will be added to the bundle. In a release configuration, jquery-1.7.1.min.js will be added. The bundling framework follows several common conventions such as: Selecting ".min" file for release when FileX.min.js and FileX.js exist.


1 Answers

This functionality is not built-in, but is possible with a couple of small helper methods. Here's what I'm using right now:

public static class Cdn {     private const string CdnRoot = "//cloudfrontdomainhere.com";      private static bool EnableCdn     {         get         {             bool enableCdn = false;             bool.TryParse(WebConfigurationManager.AppSettings["EnableCdn"], out enableCdn);             return enableCdn;         }     }      public static IHtmlString RenderScripts(string bundlePath)     {         if (EnableCdn)         {             string sourceUrl = CdnRoot + Scripts.Url(bundlePath);             return new HtmlString(string.Format("<script src=\"{0}\"></script>", sourceUrl));         }          return Scripts.Render(bundlePath);     }      public static IHtmlString RenderStyles(string bundlePath)     {         if (EnableCdn)         {             string sourceUrl = CdnRoot + Styles.Url(bundlePath);             return new HtmlString(string.Format("<link href=\"{0}\" rel=\"stylesheet\" />", sourceUrl));         }          return Styles.Render(bundlePath);     } } 

Note that I have my own configuration setting called EnableCdn in the appSettings section of my config file. When called from a Razor view, this produces the correct ouput, which appends the CDN domain onto the paths.

In your Razor files just do Cdn.RenderScripts("~/pathtoscriptbundle")

like image 195
Brian Vallelunga Avatar answered Sep 23 '22 01:09

Brian Vallelunga