Using CDN in ASP.NET MVC Packages

I read an article about linking and monetization , especially about using CDN, but there are some things that are not clear to me.

Example:

public static void RegisterBundles(BundleCollection bundles) { //bundles.Add(new ScriptBundle("~/bundles/jquery").Include( // "~/Scripts/jquery-{version}.js")); bundles.UseCdn = true; //enable CDN support //add link to jquery on the CDN var jqueryCdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"; bundles.Add(new ScriptBundle("~/bundles/jquery", jqueryCdnPath).Include( "~/Scripts/jquery-{version}.js")); // Code removed for clarity. } 
  • Is it possible to use the {version} format for CDN links, for example, for "local" ones?

  • What is the point of including in an already released version of a script like jquery-1.7.1.min.js? What if this does not exist? Should I not search for the .min file and / or generate it accordingly?

+8
jquery c # asp.net-mvc asp.net-mvc-4
source share
3 answers
 using System.Web; using System.Web.Optimization; namespace MvcApp { public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle("~/bundles/jquery", "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js").Include("~/Scripts/jquery-{version}.js")); bundles.Add(new ScriptBundle("~/bundles/bootstrap","https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js").Include("~/Scripts/bootstrap.js")); bundles.Add(new StyleBundle("~/Content/css", "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css").Include("~/Content/bootstrap.css")); BundleTable.EnableOptimizations = true; bundles.UseCdn = true; } } } 

Many developers do not understand that there is an overload for the constructor of the ScriptBundle and StyleBundle classes, which takes two string parameters, for example, for ScriptBundle it will be ScriptBundle (string, string) and for StyleBundle it will be StyleBundle (string, string). The first parameter is the virtual path, and the second parameter is cdnPath.

We can ask ourselves if it takes two parameters, how does MVC know which one to use? Well, the cdn location is only used when the BundleTable.EnableOptimizations property is set to true.

Setting the EnableOptimization property to true tells MVC to use a mini version of the file instead of the regular version.

If this property is set to true and the cdn path is present, MVC will use the cdn path instead of the local virtual path.
There is another property that you must set to true, and that is bundles.UseCdn.
This tells MVC to use the cdn location instead of the local version. If the BundleTable.EnableOptimization parameter is set to false, then the local version is automatically used as a rollback, because the cdn version is a shortened version .

Read this blog to understand what you think:

http://www.techjunkieblog.com/2015/06/aspnet-mvc-5-configure-bundleconfig.html

+2
source share
  • You cannot know this for me. But you can save the cdns table and populate when packages are loaded. When the new version that you want to use comes out, add / replace the entry in db.

      //get from db List<string> cdns = new List<string>(); foreach (string cdn in cdns) { bundles.Add(new ScriptBundle("~/bundles/jquery",cdn).Include("~/Scripts/jquery-{version}.js")); } 
  • I agree with part of the min. Since there is no part of the question, scroll down and read "Using CDN." Here is an example to show how to check. You essentially have to have a local copy as a backup, you can reference another cdn, I suppose.

0
source share

Is it possible to use the format {version} of CDN links, as for "local" ones?

The {version} placeholder is primarily designed to save time by dialing an explicit number so that the assembly can search for files on the local disk. Since the same search cannot be performed on the remote server, you will need to specify a specific URL explicitly.

What is the point of including in a bundle an already mined version of a script, for example jquery-1.7.1.min.js? What if it does not exist?

A key advantage of this binding syntax is the conditional switching between different URLs for the script and style tags in the final HTML.

If the requested file does not exist, the linking process will skip it.

If it is not looking, does the .min file exist and / or generate it accordingly? Yes, it applies minimization before grouping, as you can see: enter image description here

0
source share

All Articles