C# 自定义OutputCache提供程序在Add()和Set()上生成不同的键

C# 自定义OutputCache提供程序在Add()和Set()上生成不同的键,c#,asp.net-mvc-3,outputcache,C#,Asp.net Mvc 3,Outputcache,我正在用MVC3编写一个自定义OutputCacheProvider 调用按以下顺序触发:get、Add、Set。我希望在所有方法中,生成的键都是相同的,但它们不是。问题在于,在不同的调用中,Get和Add的键与Set方法中的键不同 我的请求如下:http://localhost/testCall?startIndex=0&maxResults=25&testParam=4 使用VaryByParam集,我希望根据我的查询参数,键是唯一的,看起来像:testCall?startIndex=0&m

我正在用MVC3编写一个自定义OutputCacheProvider

调用按以下顺序触发:get、Add、Set。我希望在所有方法中,生成的键都是相同的,但它们不是。问题在于,在不同的调用中,Get和Add的键与Set方法中的键不同

我的请求如下:
http://localhost/testCall?startIndex=0&maxResults=25&testParam=4

使用VaryByParam集,我希望根据我的查询参数,键是唯一的,看起来像:
testCall?startIndex=0&maxResults=25&testParam=4

相反,在Get/Add调用中,键只有完整的路径名:
localhost/testCall

但是,在Set调用中,键实际上看起来像我期望的那样:
local/testCallHQNmaxresultsV25NstartindexV0NtestparamV4FCDE

这是我的控制器方法

[OutputCache(Duration = 15, VaryByParam = "*")]
public ActionResult TestOutputCache()
{
    var obj = new List<string>() {"test", "one", "two", "three"};
    return Json(obj);
}

如果Set调用使用正确的参数,但Get()从未使用正确的键调用,那么缓存将只在/testCall的根调用上工作。Set方法实现中的键参数已编码,您应该对其进行解码。您可以使用HttpUtility.UrlDecode方法

   public override object Add(string key, object entry, DateTime utcExpiry)
        {
            var decodedKey = HttpUtility.UrlDecode(key)
              // implement your caching logic here 
            return entry;
        }


关键参数始终与其他参数一起出现在Set方法中-这是正常的。
outputcachedeprovider
使用此处定义的
CreateOutputCachedItemKey
方法:


如果您通读该方法的代码,您可以看到它是如何组合键的,这解释了键中额外的字母和查询字符串项。

Hello@davideath我遇到了相同的问题。你有这方面的更新吗?同样的问题。密钥是如何生成的??我们在“Set”方法中看到的wierd字符是什么?
   public override object Add(string key, object entry, DateTime utcExpiry)
        {
            var decodedKey = HttpUtility.UrlDecode(key)
              // implement your caching logic here 
            return entry;
        }