. NET MVC extension Urlhelper support CDN

Source: Internet
Author: User

0x00, why do you want to extend

Because my server is a small water pipe, loading a complete website often takes a long time, want to speed up the site loading speed, static files are best to separate out, all think of the extension urlhelper, used to support CDN loading files.

0X01, on several methods of quoting static files

jquery-1.11.0.min.jsFor example, the following two types are commonly used (my own case)

<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("")Form is the method of Urlhelper, we will extend it today

0x02, extended Code

Create a new UrlHelperExtensions class

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 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;        }    }}

Also add a configuration to the appsettings node in Web. config

123456789 <!--是否开启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, extending the use of

Directly on the page @url.cdncsscontent ("") or @Url. Cdnjscontent ("") or @Url. Cdnimgcontent (""), which is my page reference

0x04, Effect

0x05, Cdn/oss settings

Here, the source address is set to the address of the main station, so that when the CDN cannot find the file, it will automatically pull the file from the master station

It is recommended to open the anti-theft chain referer, and set the

Not well written, please advise me

. NET MVC extension Urlhelper support CDN

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.