开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜