加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-5 – 如果param是列表,则VaryByParam失败

发布时间:2020-12-16 09:40:40 所属栏目:asp.Net 来源:网络整理
导读:我在MVC中有这个动作 [OutputCache(Duration = 1200,VaryByParam = "*")]public ActionResult FilterArea( string listType,Listint designersID,int currPage = 1 ){ // Code removed} 无法像url一样呈现正确的HTML http://example.com/en-US/women/clothin
我在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
> http://example.com/en-US/women/clothing?designersID=158&designersID=13

这是一个知道的.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);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读