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.js
For 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