忽略用的OutputCache URL值 [英] Ignore url values with Outputcache

查看:154
本文介绍了忽略用的OutputCache URL值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用outputcaching它是由完整的URL缓存的操作方法,我想这样做的是缓存中的网页,但忽略网址的某些部分。

When I use outputcaching on an action method it is cached by full url, what i would like to do is cache the page but ignore some parts of the url.

自定义路线:

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 */
   }
}

例如,当我去到以下网址:

For example when I go to the following URL's:

http://mywebsite.com/Report/789/cacheme/Template/5 结果
- >缓存基于链接2小时

http://mywebsite.com/Report/789/cacheme/Template/5
-> cached for 2 hours based on the url

http://mywebsite.com/Report/777/anothercacheme/Template/5 结果
- >也将被缓存基于URL2小时

http://mywebsite.com/Report/777/anothercacheme/Template/5
-> also gets cached for 2 hours based on that url

我想的是,在的OutputCache忽略REPORTNAME和reportid值所以,当我去到上述网址的它返回的相同的缓存版本。这可能与的OutputCache属性,或者我会写我的自定义的OutputCache FilterAttribute?

What I would like is that the OutputCache ignores the reportname and reportid values so when I go to the above url's it returns the same cached version. Is this possible with the OutputCache attribute or will I have to write my custom OutputCache FilterAttribute?

推荐答案

以下的(通过的http://blog.stevensanderson.com/2008/10/15/partial-output-caching-in-aspnet-mvc/):

Ended up with the following (inspired by 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);
        }
    }

这篇关于忽略用的OutputCache URL值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆