How to host asp.net mvc4 jqueryval on cdn?

309 views Asked by At

As Asp.Net MVC4 supports script bundling, it is very convenient to use something like

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
            "~/Scripts/jquery.unobtrusive*",
            "~/Scripts/jquery.validate*"));

However, this bundle includes several files so if I want to use CDN (like Microsoft one - http://www.asp.net/ajaxlibrary/cdn.ashx) I would need to reference 3 different files.

Unfortunately, ScriptBundle constructor takes only 1 CDN path. How do I keep bundling enabled for local development and use CDN for production? What is the most efficient way?

2

There are 2 answers

1
Esteban Araya On

If you want to use a CDN for each of those deps, you have to create a bundle for each script.

For what it's worth, I think it's kind of crazy to trust a 3rd party, over which you have no control, to serve your assets. Just serve the files yourself; it's not worth the hassle.

0
Axefield On

You will indeed need to create 2 bundles, each refering to another CDN.

When using .NET 4.5 you can easily increase the robustness by adding a fallback CDN as Scott Hanselman describes: http://www.hanselman.com/blog/CDNsFailButYourScriptsDontHaveToFallbackFromCDNToLocalJQuery.aspx

To find some of the fallback expressions checkout this article: http://geekswithblogs.net/cskardon/archive/2014/07/04/cdnfallbackexpression-for-jquery-jquery.ui-modernizr-and-bootstrap.aspx