Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC4 bundling CSS Some on CDN some local to server?

I am looking into the ASP.NET MVC4 System.Web.Optimization bundling and was wondering how would you go about serving up some CSS files from a CDN and others local to a server?

Is that possible?

It looks like the bundles.UseCdn = true is at the collection level and not a setting for an individual bundle.

Any guidance would be appreciated.

like image 276
Ken Burkhardt Avatar asked Nov 09 '12 14:11

Ken Burkhardt


People also ask

How do you override the bundling setting of web config?

To enable bundling and minification, set the debug value to "false". You can override the Web. config setting with the EnableOptimizations property on the BundleTable class. The following code enables bundling and minification and overrides any setting in the Web.

What are the different ways for bundling and minification in asp net core?

Bundling and minification are two techniques you can use in ASP.NET to improve page load performance for your web application. Bundling combines multiple files into a single file. Minification performs a variety of different code optimizations to scripts and CSS, which results in smaller payloads.

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.


1 Answers

The ASP.NET site has some information about this:

The following code replaces the local jQuery bundle with a CDN jQuery bundle.

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.
}

In the code above, jQuery will be requested from the CDN while in release mode and the debug version of jQuery will be fetched locally in debug mode. When using a CDN, you should have a fallback mechanism in case the CDN request fails. The following markup fragment from the end of the layout file shows script added to request jQuery should the CDN fail.

Personally, I don't find myself debugging code from jQuery, Knockout, or any other library. I always reference the CDN resource directly in my layout. Any scripts I need to roll myself I then bundle using the MVC framework.

My sites usually look something like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
@Scripts.Render("~/content/js/siteName")
like image 72
Justin Helgerson Avatar answered Nov 15 '22 23:11

Justin Helgerson