ASP.NET MVC with multiple virtual paths with CDN
I am trying to add a CDN compatible package with ASP.NET MVC 4. The goal is to share content locally with many other sites hosted in the same datacenter
First try:
bundles.Add(new ScriptBundle("~/bundles/jquery", "http://mysite/Content/js/").Include(
"http://mycdnsite/Content/js/jquery.unobtrusive-ajax.min.js",
"http://mycdnsite/Content/js/jquery-migrate-1.2.1.js",
"http://mycdnsite/Content/js/jquery-{version}.js"));
Unfortunately this is not possible, because virtualPaths must be relative (Only relative app urls (~ / url) are allowed)
Then I tried this:
bundles.Add(new ScriptBundle("~/bundles/jquery", "http://mycdnsite/Content/js/").Include(
"~/jquery.unobtrusive-ajax.min.js",
"~/jquery-migrate-1.2.1.js",
"~/jquery-{version}.js"));
But it didn't work, even turning on the CDN:
BundleTable.EnableOptimizations = true;
bundles.UseCdn = true;
Is it possible to create multiple content packages from a CDN?
+3
source to share
2 answers
AFAIK you cannot serve multiple CDN hosts in the same bundle. ScriptBundle
allows you to specify an alternate URL for a package, and a package can contain multiple local files. The syntax you have is correct.
bundles.UseCdn = true;
bundles.Add(new ScriptBundle("~/bundles/jquery",
@"//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.js"
).Include(
"~/Scripts/jquery-{version}.js"));
There are several ways to solve this problem.
- You have one package per CDN hosted script.
- Manually bundle the files and upload them to your own CDN and specify that.
+1
source to share
public static void RegisterBundles(BundleCollection bundles)
{
bundles.UseCdn = true; //enable CDN
// How To add link to jquery on the CDN
var jquryCdnPath = "http://mycdnsite/Content/js/jquery.unobtrusive-ajax.min.js";
bundles.Add(new ScriptBundle("~/bundles/jquery",
jquryCdnPath).Include(
"~/Scripts/jquery-{version}.js"));
}
0
source to share