. Net mvc extension UrlHelper supports CDN and mvcurlhelper
0x00. Why?
Because my server is a small water pipe, it often takes a long time to load a complete website. To speed up website loading, it is best to separate static files. All of them come up with the idea of extending UrlHelper, it is used to support loading files through CDN.
0x01. Several Methods for referencing static files
Tojquery-1.11.0.min.js
For example, there are two common examples (my own cases)
<script src="~/Content/themes/plugins/jQuery/jquery-1.11.0.min.js"></script><script src="@Url.Content("~/Content/themes/plugins/jQuery/jquery-1.11.0.min.js")"></script>
@Url.Content("")
The form is the UrlHelper method. We will expand it today.
0x02, extended code
CreateUrlHelperExtensions
Class
using System;using System.Collections.Generic;using System.Configuration;using System.Linq;using System.Web;using System.Web.Mvc;namespace Chenmo.Soft.WebUI.Framework{ public static class UrlHelperExtensions { /// <summary>CSS cdn /// /// </summary> /// <param name="helper"></param> /// <param name="contentPath"></param> /// <returns></returns> public static string CdnCssContent(this UrlHelper helper, string contentPath) { return GetContent(helper, contentPath, "CSS"); } /// <summary>JS cdn /// /// </summary> /// <param name="helper"></param> /// <param name="contentPath"></param> /// <returns></returns> public static string CdnJsContent(this UrlHelper helper, string contentPath) { return GetContent(helper, contentPath, "JS"); } /// <summary>img cdn /// /// </summary> /// <param name="helper"></param> /// <param name="contentPath"></param> /// <returns></returns> public static string CdnImgContent(this UrlHelper helper, string contentPath) { return GetContent(helper, contentPath, "IMG"); } private static string GetContent(this UrlHelper helper, string contentPath, string type) { var result = helper.Content(contentPath); if (ConfigurationManager.AppSettings[$"CDN_{type}_Enable"].ToUpper() == "TRUE") { result = ConfigurationManager.AppSettings[$"CDN_{type}_URL"] + contentPath.TrimStart('~'); } return result; } }}
Add the configuration to the appSettings node in web. config.
<! -- Whether to enable CDN True False --> <add key = "CDN_CSS_Enable" value = "True"/> <add key = "CDN_CSS_URL" value = "http://css.static.ofnhkb1.com"/> <add key = "CDN_JS_Enable" value = "True"/> <add key = "CDN_JS_URL" value = "http://js.static.ofnhkb1.com"/> <add key = "CDN_IMG_Enable" value = "True"/> <add key = "CDN_IMG_URL" value = "http://img.static.ofnhkb1.com"/>
0x03. Extended use
Directly go to the page, @ Url. CdnCssContent ("") or @ Url. CdnJsContent ("") or @ Url. CdnImgContent (""), which is a reference to my page.
0x04, Effect
0x05, CDN/OSS settings
Here we mention that the Back-to-source address is set as the address of the master site, so that when the cdn cannot find the file, it will automatically pull the file from the master site
We recommend that you enable anti-leech Referer and set it.
Not well written. Please give me more advice.