Specifiying Include Path and CdnFallbackExpression in single ScriptBundle command? RRS feed

  • Question

  • User-2145974911 posted

    I am trying to use a CDN in my bundle config to server up jQuery but also want to provide a local fallback. I found http://venkatbaggu.com/use-cdn-bundle-config-in-asp-net-mvc/ which shows how to use the CdnFallbackExpression to do this but it has you build out a variable, add the fallback data to the variable, then add the built out variable to the bundles.  What I was wondering is if there was a way to do this all within the bundle.add() command instead of having to build out a special variable for the jQuery bundle? I already have

    Bundles.Add(new ScriptBundle("~/Scripts/jQuery", @"//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js").Include("~/Scripts/jquery-{version}.js"));

    which works fine but can not figure out how to add the CdnFallbackExpression to my existing line of code.

    Thursday, November 13, 2014 9:39 PM

All replies

  • User1100692814 posted

    Hi Eagle_f90,

    In the latest release, the Bundle class includes the property CdnFallbackExpression.

    If you render a bundle that uses a CDN with the CndFallbackExpression specified, the framework renders a bit of script into the DOM for the fallback check. For example, if you setup your bundle as follows,

    bundles.UseCdn = true;
    var jqueryCdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js";
    var bundle = new ScriptBundle("~/scripts/jquery", jqueryCdnPath)
        CdnFallbackExpression = "window.jquery"

    To load jquery from ajax.aspnetcdn.com. The framework will insert:

    <script src=”http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"><\/script>
        "<script src=”/scripts/jquery"><\/script>")

    Hope this helps.


    Monday, March 5, 2018 11:09 AM