.NET MVC扩展UrlHelper支持CDN

0x00、为什么要扩展

  因为我的服务器是小水管,加载一个完整的网站往往需要很久,想加速网站加载速度,静态文件最好是分离出来,所有就想到了扩展UrlHelper,用来支持CDN加载文件。

0x01、论引用静态文件的几种方法

以 jquery-1.11.0.min.js 为例,一般常用的有以下两种(我自己的情况)

<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("") 形式是UrlHelper的方法,我们今天就来扩展它

0x02、扩展的代码

新建一个UrlHelperExtensions 类


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

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;

        }

    }

}

  同时在web.config 中的appSettings节点添加一下配置


1

2

3

4

5

6

7

8

9

<!--是否开启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、扩展的使用

  直接在页面上@Url.CdnCssContent("") or @Url.CdnJsContent("") or @Url.CdnImgContent("") 即可,如图是我的页面引用

0x04、效果

0x05、CDN/OSS设置

  这里提一点,把回源地址设置为主站的地址,这样当cdn找不到文件的时候,会自动从主站拉取文件

  建议把防盗链Referer给打开,并设置好

  写得不好,请各位多多指教

时间: 2024-08-12 10:14:21

.NET MVC扩展UrlHelper支持CDN的相关文章

面向接口可扩展框架之“Mvc扩展框架及DI”

面向接口可扩展框架之“Mvc扩展框架及DI” 标题“Mvc扩展框架及DI”有点绕口,我也想不出好的命名,因为这个内容很杂,涉及多个模块,但在日常开发又密不可分 首先说Mvc扩展框架,该Mvc扩展就是把以前的那个Mvc分区扩展框架迁移过来,并优化整合了一下 一.Mvc扩展框架主要功能: 1.Mvc的依赖注入(DI)功能(类MvcDependency) 依赖IContainerFactory接口,不再依赖具体容器 2.Mvc全局过滤器(GlobalFilterProvider) 配置在Mvc的依赖注

Asp.net 面向接口可扩展框架之“Mvc扩展框架及DI”

标题“Mvc扩展框架及DI”有点绕口,我也想不出好的命名,因为这个内容很杂,涉及多个模块,但在日常开发又密不可分 首先说Mvc扩展框架,该Mvc扩展就是把以前的那个Mvc分区扩展框架迁移过来,并优化整合了一下 一.Mvc扩展框架主要功能: 1.Mvc的依赖注入(DI)功能(类MvcDependency) 依赖IContainerFactory接口,不再依赖具体容器 2.Mvc全局过滤器(GlobalFilterProvider) 配置在Mvc的依赖注入容器中就能自动易用上,其实逻辑很简单,就是继

改变mvc web api 支持android ,ios ,ajax等方式跨域调用

公司一个移动后端的项目用到了 webapi 项目搭建到外网环境共app开发者调用测试接口时遇到了一个问题 接口不允许跨域调用 .查阅资料明白 同源策略原则根据请求报头值 Origin 与回应报头值 Access-Control-Allow-Origin 来判断是否允许调用 解决方法 1.ajax使用jsonp jsonp 是通过请求参数中加入回调函数参数值.webapi 收到回调函数参数值返回数据不再是单纯的json,而是根据回调函数参数值 的js方法调用,这样就避免的同源策略 需要webapi

MVC扩展控制器工厂,通过继承DefaultControllerFactory来决定使用哪个接口实现,使用Ninject

希望实现的效果是:对购物车中所有商品的总价,实现9折或8折: 当点击"9折": 当点击"8折": □ 思路 8折或9折是打折接口的不同实现,关键是:由什么条件决定使用哪种打折方式? --当点击8折或9折链接的时候,把参数放在路由中,然后在自定义控制器工厂中根据参数的不同选择使用哪种打折方式. □ model public class CartLine    {        public int Id { get; set; }        public stri

MVC扩展DataAnnotationsModelMetadataProvider给model属性对应的页面元素添加任意属性和值

比如,有这样一个类: public class User    {        public string Name { get; set; }    } 当在强类型视图页,显示属性Name对应的input元素,并想添加一个title属性和对应的值,如图: □ 思路 →自定义TooltipAttribute,可以打到Name属性上.→自定义DataAnnotationsModelMetadataProvider,把TooltipAttribute的Tooltip属性值放到放到ModelMeta

MVC扩展Filter,通过继承HandleErrorAttribute,使用log4net或ELMAH组件记录服务端500错误、HttpException、Ajax异常等

□ 接口 public interface IExceptionFilter{    void OnException(ExceptionContext filterContext);} ExceptionContext继承于ControllerContext,从中可以获得路由数据route data.HttpContext. □ 的HandleErrorAttribute是对IExceptionFilter的实现,默认是启用的 public static void RegisterGlobal

MVC扩展控制器工厂,通过实现IControllerFactory

关于控制器工厂的扩展,要么通过实现IControllerFactory接口,要么通过继承DefaultControllerFactory.本篇中,我想体验的是: 1.当请求经过路由,controller, action名称是以key/value键值对形式存放的,我们可以通过RequestContext.RouteData.Values["action"]和RequestContext.RouteData.Values["controller"]获取action或co

MVC扩展ModelBinder,通过继承DefaultModelBinder把表单数据封装成类作为action参数

把视图省.市.街道表单数据,封装成一个类,作为action参数.如下: action方法参数类型: namespace MvcApplication1.Models{    public class Customer    {        public string Address { get; set; }    }} 在自定义ModelBinder中,接收视图表单数据,封装成Customer类. using System.Web; using System.Web.Mvc; using M

ahjesus 让我的MVC web API支持JsonP跨域

无数被跨域请求爆出翔来的人 遇到请求成功却不能进入success 总是提示parsererror 参考一下两篇文章吧 参考文章http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api and http://diaosbook.com/Post/2013/12/27/tips-for-aspnet-webapi-cors ahjesus 让我的MVC web API支持JsonP跨域