• Asp.net Mvc中扩展ActionFilterAttribute修改Http Cache Header


    在asp.net mvc3中扩展ActionFilterAttribute实现Http Cache Header修改

    ActionFilterAttribute 是 System.Web.Mvc.ActionFilterAttribute

    来自于:System.Web.Mvc.dll

    #region Assembly System.Web.Mvc.dll, v4.0.30319
    // C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll
    #endregion
    
    using System;
    
    namespace System.Web.Mvc
    {
        // Summary:
        //     Represents the base class for filter attributes.
        [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
        public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter, IResultFilter
        {
            // Summary:
            //     Initializes a new instance of the System.Web.Mvc.ActionFilterAttribute class.
            protected ActionFilterAttribute();
    
            // Summary:
            //     Called by the ASP.NET MVC framework after the action method executes.
            //
            // Parameters:
            //   filterContext:
            //     The filter context.
            public virtual void OnActionExecuted(ActionExecutedContext filterContext);
            //
            // Summary:
            //     Called by the ASP.NET MVC framework before the action method executes.
            //
            // Parameters:
            //   filterContext:
            //     The filter context.
            public virtual void OnActionExecuting(ActionExecutingContext filterContext);
            //
            // Summary:
            //     Called by the ASP.NET MVC framework after the action result executes.
            //
            // Parameters:
            //   filterContext:
            //     The filter context.
            public virtual void OnResultExecuted(ResultExecutedContext filterContext);
            //
            // Summary:
            //     Called by the ASP.NET MVC framework before the action result executes.
            //
            // Parameters:
            //   filterContext:
            //     The filter context.
            public virtual void OnResultExecuting(ResultExecutingContext filterContext);
        }
    }

    以上是ActionFilterAttribute 类的具体代码,它是抽象类 abstract  ,有四个虚方法 virtual .

    User Story:

         作为一个用户, 希望系统可以让我在需要访问系统时能够有快的速度, 以便我能够看到信息,提高我的体验。

    为系统添加缓存:

    给http header添加缓存值,使用其它缓存系统对web进行缓存,使用合适的缓存策略。

    Cache-Control:max-age=value

    E-TTL=value

    1.扩展ActionFilterAttribute

       CacheHeaderFilter

        using System;
        using System.Web;
        using System.Web.Mvc;
    
        /// <summary>
        /// The cache filter.
        /// </summary>
        public class CacheHeaderFilter : ActionFilterAttribute
        {
            /// <summary>
            /// ETTL
            /// </summary>
            public int ETTL { get; set; }
    
            /// <summary>
            /// CacheControlMaxAge
            /// </summary>
            public int CacheControlMaxAge { get; set; }
    
            /// <summary>
            /// OnActionExecuted
            /// </summary>
            /// <param name="filterContext"></param>
            public override void OnActionExecuted(ActionExecutedContext filterContext)
            {
                var response = filterContext.HttpContext.Response;
                response.Cache.SetCacheability(HttpCacheability.Public);
                response.Cache.SetMaxAge(TimeSpan.FromSeconds(this.CacheControlMaxAge));
                response.Headers.Add("E-TTL", this.ETTL.ToString());
            }

    在Asp.net MVC 中使用 CacheHeaderFilter

            /// <summary>
            /// index page
            /// </summary>
            /// <returns></returns>
            [CacheHeaderFilter(CacheControlMaxAge = 3600, ETTL = 3600)]
            public ActionResult Index()
            {
                return View();
            }

    111414

    作者:HackerVirus
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    版权信息:HackerVirus@2013
  • 相关阅读:
    后台返回null iOS
    iOS代码规范
    没落的公司 该何去何从
    定位框一闪而过 iOS Swift
    根据appid跳到App Store某个APP的详情页
    Analyze 静态分析内存泄漏代码优化 instrument之Xcode
    bugly手动上传符号表和自动上传符号表
    __null_unspecified属性关键字解决内存泄漏
    栅栏函数dispatch_barrier_sync/async
    cell左滑加删除置顶功能 iOS
  • 原文地址:https://www.cnblogs.com/Hacker2012/p/2944543.html
Copyright © 2020-2023  润新知