首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Outputcache忽略url值

使用Outputcache忽略url值
EN

Stack Overflow用户
提问于 2011-04-14 08:11:19
回答 1查看 819关注 0票数 1

当我在一个动作方法上使用输出缓存时,它是由完整的url缓存的,我想要做的是缓存页面,但忽略url的某些部分。

在Global.asax中定义的自定义路由:

代码语言:javascript
复制
routes.MapRoute(
  "Template",
  "Report/{reportid}/{reportname}/Template/{templateid}/{action}",
  new { controller = "Template", action = "Index" }
);

我的模板控制器

代码语言:javascript
复制
public class TemplateController : Controller
{
   [OutputCache(Duration=60*60*2)]
   public ActionResult Index(Template template)
   {
      /* some code */
   }
}

例如,当我转到以下URL时:

http://mywebsite.com/Report/789/cacheme/Template/5

基于url的->缓存2小时

http://mywebsite.com/Report/777/anothercacheme/Template/5

->还将根据该url缓存2小时。

我想要的是,OutputCache忽略了reportname,并报告了值,所以当我转到上面的url时,它会返回相同的缓存版本。使用OutputCache属性可以这样做吗?还是我必须编写我的自定义OutputCache FilterAttribute?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-18 08:34:52

): 以以下方式结束(灵感来自

代码语言:javascript
复制
 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5660319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档