. Net mvc extension UrlHelper supports CDN and mvcurlhelper

Source: Internet
Author: User

. 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.jsFor 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

CreateUrlHelperExtensionsClass

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.