CDN path not working in js bundling in MVC

I am working on ASP.NET MVC4. The client must download all javascripts and css from other domains, such as CDN, using the bundle. I 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(); 

in view

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

But this is not rendering from another domain.

What could be the problem?

+4
source share
2 answers

This example shows how to load a resource from a 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 the resource you want to get from the CDN, and Include indicates where to find it. You can change which one is requested in Web.config. Set <compilation debug="true"/> to use the local file and <compilation debug="false"/> to use the CDN.

See the Bundling and Minification article for more information.

+5
source

I don’t care how cdnPath works in the bundle, because you can specify only one file path for the whole package. If you want to configure a simple source code 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 package with a lot of different files, regardless of whether optimization is enabled.

This also concerns the issue with the query parameter described here.

+1
source

Source: https://habr.com/ru/post/1485964/


All Articles