忽略URL值与Outputcache

问题描述

| 当我在一个动作方法上使用outputcaching时,它会被完整的url缓存,我想做的是缓存页面,但是忽略了url的某些部分。 Global.asax中定义的自定义路由:
routes.MapRoute(
  \"Template\",\"Report/{reportid}/{reportname}/Template/{templateid}/{action}\",new { controller = \"Template\",action = \"Index\" }
);
我的模板控制器
public class TemplateController : Controller
{
   [OutputCache(Duration=60*60*2)]
   public ActionResult Index(Template template)
   {
      /* some code */
   }
}
例如,当我转到以下网址时: http://mywebsite.com/Report/789/cacheme/Template/5 ->根据URL缓存2小时 http://mywebsite.com/Report/777/anothercacheme/Template/5 ->也会根据该网址缓存2小时 我想要的是OutputCache忽略reportname和reportid值,因此当我转到上述url \时,它将返回相同的缓存版本。 OutputCache属性是否可行?还是必须编写自定义OutputCache FilterAttribute?     

解决方法

最终得到以下内容(灵感来自http://blog.stevensanderson.com/2008/10/15/partial-output-caching-in-aspnet-mvc/):
 public class ResultCacheAttribute : ActionFilterAttribute
    {
        public ResultCacheAttribute()
        {

        }

        public string CacheKey
        {
            get;
            private set;
        }

        public bool AddUserCacheKey { get; set; }
        public bool IgnoreReport { get; set; }

        /// <summary>
        /// Duration in seconds of the cached values before expiring.
        /// </summary>
        public int Duration
        {
            get;
            set;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string url = \"\";
            foreach (var item in filterContext.RouteData.Values)
            {
                if (IgnoreReport)
                    if (item.Key == \"reportid\" || item.Key == \"reportname\")
                        continue;

                url += \".\" + item.Value;
            }
            if (AddUserCacheKey)
                url += \".\" + filterContext.HttpContext.User.Identity.Name;

            this.CacheKey = \"ResultCache-\" + url;

            if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
                this.CacheKey += \"-ajax\";

            if (filterContext.HttpContext.Cache[this.CacheKey] != null)
            {
                filterContext.Result = (ActionResult)filterContext.HttpContext.Cache[this.CacheKey];
            }
            else
            {
                base.OnActionExecuting(filterContext);
            }
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            filterContext.Controller.ViewData[\"CachedStamp\"] = DateTime.Now;
            filterContext.HttpContext.Cache.Add(this.CacheKey,filterContext.Result,null,DateTime.Now.AddSeconds(Duration),System.Web.Caching.Cache.NoSlidingExpiration,System.Web.Caching.CacheItemPriority.Default,null);

            base.OnActionExecuted(filterContext);
        }
    }
    

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...