5

I'm working on ASP.NET MVC4. Client has requirement to load all javascripts and css from other domains like CDN with bundling. I've used System.Web.Optimization.

below is the code.

 var bundle = new ScriptBundle("~/bundles/scripts/");
bundle.Orderer = new AsIsBundleOrderer();
bundle.EnableFileExtensionReplacements = false;
bundle.CdnPath = "http://js.cusomdomainname.com";
bundle.Include("~/Scripts/jquery-1.7.1.min.js",
                "~/Scripts/jquery.unobtrusive-ajax.min.js",
                "~/Scripts/jquery.validate.min.js",
                "~/Scripts/jquery.validate.unobtrusive.min.js");

BundleTable.Bundles.UseCdn = true;
BundleTable.EnableOptimizations = true;
BundleTable.Bundles.Add(bundle);
BundleTable.Bundles.IgnoreList.Clear();

on view

@Scripts.Render("~/bundles/scripts/")

But It's is not rendering from another domain.

What could be the problem?

tereško
  • 58,060
  • 25
  • 98
  • 150
Dharmik Bhandari
  • 1,702
  • 5
  • 29
  • 60
  • Here is the similar question..answered http://stackoverflow.com/questions/15434692/javascript-error-in-asp-net-mvc-4-bundling – Hakuna Matata Jun 13 '13 at 09:47

2 Answers2

5

This example shows how to load resource from CDN in 'release' mode and locally from 'debug' mode.

var jqueryCdnPath = "//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js";
var jqueryBundle = new ScriptBundle("~/bundles/jqueryCdn", jqueryCdnPath)
                       .Include("~/Scripts/jquery-{version}.js");

BundleTable.Bundles.Add(jqueryBundle);

CdnPath refers to a resource you want to get from a CDN, and Include tells where to find that locally. You can change which one is requested from Web.config. Set <compilation debug="true"/> to use local file, and <compilation debug="false"/> to use CDN.

See this Bundling and Minification article for more information.

jjokela
  • 337
  • 2
  • 8
  • with your solution it's useful for jquery load multiple but also load multiple css using cdn then how can it's possible with cdn any idea about this then please let me know i needed. – coderwill May 02 '17 at 08:33
  • using bundling just got issue while testing CDN offline scenario. local script loading fails. as it output following script in page . as bundle misses to add ref.path of minified script in rendered page. It a bug in optimization library. more info can be found on https://stackoverflow.com/questions/21004913/mvc-cdn-fallback-for-style-bundle. Looks like it still not resolved. hope it save someone else time. :) – 0cool Jan 30 '20 at 16:59
1

I don't care for the way the cdnPath stuff works in bundling because you can only specify a single file path for the whole bundle. If you want to set up a simple origin-pull CDN it's much easier to do the following:

@Scripts.RenderFormat(
    "<script src='http://js.cusomdomainname.com{0}'></script>",
    "~/bundles/scripts/")

This will work if you have a bundle with lots of different files whether or not optimizations are enabled.

This also gets around the problem with the query parameter described here

Brandon Cuff
  • 1,438
  • 9
  • 24
  • Entirely agree with you. The bundles "almost works". If you can just add the CDN path and the bundler would load the included scripts from the specified path, perfect. – WoofWoof88 Aug 06 '14 at 13:16