asp.net-mvc-5 – 如果param是列表,则VaryByParam失败
我在MVC中有这个动作
[OutputCache(Duration = 1200,VaryByParam = "*")] public ActionResult FilterArea( string listType,List<int> designersID,int currPage = 1 ) { // Code removed } 无法像url一样呈现正确的HTML > http://example.com/en-US/women/clothing?designersID=158 这是一个知道的.NET中OutputCache的错误导致无法识别带有列表参数的VaryByParam或者我错过了什么? 解决方法
我在MVC3中也有同样的问题,我相信它在MVC5中仍然是相同的.
这是我的设置. 请求 POST,Content-Type:application / json,传入一个字符串数组作为参数 { "options": ["option1","option2"] } 控制器方法 [OutputCache(Duration = 3600,Location = OutputCacheLocation.Any,VaryByParam = "options")] public ActionResult GetOptionValues(List<string> options) 我尝试使用OutputCache可能的每个选项,它只是不缓存我.绑定工作正常,实际工作方法.我最大的怀疑是OutputCache没有创建唯一的缓存键,所以我甚至将其代码从System.Web.MVC.OutputCache中取出来进行验证.我已经验证它即使在List< string>传入.还有其他东西在那里,但不值得花更多的精力. OutputCacheAttribute.GetUniqueIdFromActionParameters(filterContext,OutputCacheAttribute.SplitVaryByParam(this.VaryByParam); 解决方法 我最后在另一个SO帖子之后创建了我自己的OutputCache属性.更容易使用,我可以享受一天的其余时间. 控制器方法 [MyOutputCache(Duration=3600)] public ActionResult GetOptionValues(Options options) 自定义请求类 我继承自List< string>所以我可以调用MyOutputcache类中的重写.ToString()方法来给我一个唯一的缓存键字符串.仅此方法就解决了其他问题,但对我来说却没有. [DataContract(Name = "Options",Namespace = "")] public class Options: List<string> { public override string ToString() { var optionsString= new StringBuilder(); foreach (var option in this) { optionsString.Append(option); } return optionsString.ToString(); } } 自定义OutputCache类 public class MyOutputCache : ActionFilterAttribute { private string _cachedKey; public int Duration { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.HttpContext.Request.Url != null) { var path = filterContext.HttpContext.Request.Url.PathAndQuery; var attributeNames = filterContext.ActionParameters["Options"] as AttributeNames; if (attributeNames != null) _cachedKey = "MYOUTPUTCACHE:" + path + attributeNames; } if (filterContext.HttpContext.Cache[_cachedKey] != null) { filterContext.Result = (ActionResult) filterContext.HttpContext.Cache[_cachedKey]; } else { base.OnActionExecuting(filterContext); } } public override void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.HttpContext.Cache.Add(_cachedKey,filterContext.Result,null,DateTime.Now.AddSeconds(Duration),System.Web.Caching.Cache.NoSlidingExpiration,System.Web.Caching.CacheItemPriority.Default,null); base.OnActionExecuted(filterContext); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 带有ASP.NET MVC的通配符MapPageRoute支持组
- 休息 – ASP.NET Web Api路由自定义
- asp.net-web-api – OWIN中间件中的全局异常处理
- asp.net – 在Page_Load(对象发送者,EventArgs e)和$(docum
- asp.net-mvc – ASP.NET MVC 1.0中的表单输入验证选项
- asp.net-web-api – Web API嵌套路由无法按预期工作
- Asp.net初识
- Asp.net C#具有特殊字符的电子邮件地址
- .net – MVC3 HttpPostedFileBase首先上传没有数据,但后来做
- 如何排序. .NET中的resx(资源文件)
- asp.net-mvc – MVC DropDownList SelectedValue
- ASP.NET Core中调整HTTP请求大小的几种方法详解
- 如何将Bitcoin比特币区块链数据导入关系数据库
- asp.net-core – 程序集中找不到入口点’Microso
- ASP.NET – meta:ResourceKey vs <%$Resources
- asp.net-mvc – 如何在ASP.NET MVC中添加路由到动
- ASP.NET c#获取屏幕宽度(以像素为单位)
- 经典ASP会话ID Cookie生命周期
- asp.net-mvc-3 – 从控制器发送电子邮件
- 强制ASP.NET为所有用户代理生成JavaScript