开发者

ASP.NET MVC 3.0 : Configure OutputCache attribute to produce Cache-Control: public

开发者 https://www.devze.com 2023-03-01 16:17 出处:网络
I want OutputCache attribute to produce a header with Cache-Control set to public. How do开发者_StackOverflow中文版 I do that?Maybe this code for Attribute will help

I want OutputCache attribute to produce a header with Cache-Control set to public. How do开发者_StackOverflow中文版 I do that?


Maybe this code for Attribute will help

using System;
using System.Web;
using System.Web.Mvc;

    public class CacheFilterAttribute : ActionFilterAttribute {
        /// <summary>
        /// Gets or sets the cache duration in seconds. The default is 10 seconds.
        /// </summary>
        /// <value>The cache duration in seconds.</value>
        public int Duration {
            get;
            set;
        }

        public CacheFilterAttribute() {
            Duration = 30;
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext) {
            if (Duration <= 0) return;

            HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
            TimeSpan cacheDuration = TimeSpan.FromSeconds(Duration);

            cache.SetCacheability(HttpCacheability.Public);
            cache.SetExpires(DateTime.Now.Add(cacheDuration));
            cache.SetMaxAge(cacheDuration);
            cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
        }
    }

and then use [CacheFilter] instead of OutputCache

0

精彩评论

暂无评论...
验证码 换一张
取 消