VaryByParam="*" 是否有效?还阅读 RouteData.Values?
在我的 asp.net mvc 项目中,我在控制器上启用输出缓存,如下所示,
[OutputCache(Duration = 100, VaryByParam = "*", VaryByHeader = "X-Requested-With")]
public class CatalogController : BaseController
{
public ActionResult Index(string seller)
{
// I do something
}
}
它效果很好,直到创建我自己的 Route 类,如下所示
public class MyRoute : Route
{
// there is a constructor here..
// I override this method..
// just to add one data called 'seller' to RouteData
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var data = base.GetRouteData(httpContext);
if (data == null) return null;
var seller = DoSomeMagicHere();
// add seller
data.Values.Add("seller", seller);
return data;
}
}
,然后,操作方法将采用 seller
作为参数。我通过始终提供不同的 seller
参数来测试它,但它从缓存中获取输出而不是调用该方法。
在 asp.net mvc 中,设置 VaryByParam="*" 是否也会因 RouteData.Values 而异?
我正在使用 ASP.Net 4 MVC 3 RC 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
输出缓存机制因 URL、QueryString 和 Form 的不同而异。 RouteData.Values 此处未表示。原因是输出缓存模块在路由之前运行,因此当第二个请求进入并且输出缓存模块正在寻找匹配的缓存条目时,它甚至没有要检查的 RouteData 对象。
通常这不是问题,因为 RouteData.Values 直接来自 URL,而 URL 已经被考虑在内。如果您想根据某个自定义值进行更改,请使用 VaryByCustom和 GetVaryByCustomString 来完成此操作。
The output caching mechanism varies by URL, QueryString, and Form. RouteData.Values is not represented here. The reason for this is that the output caching module runs before Routing, so when the second request comes in and the output caching module is looking for a matching cache entry, it doesn't even have a RouteData object to inspect.
Normally this isn't a problem, as RouteData.Values comes straight from the URL, which is already accounted for. If you want to vary by some custom value, use VaryByCustom and GetVaryByCustomString to accomplish this.
如果您删除 VaryByParam = "*" 它应该在缓存时使用您的操作方法参数值。
来源:http://weblogs.asp.net/scottgu/archive/2010/12/ 10/announcing-asp-net-mvc-3-release-candidate-2.aspx
If you remove VaryByParam = "*" it should use your action method parameter values when caching.
Source: http://weblogs.asp.net/scottgu/archive/2010/12/10/announcing-asp-net-mvc-3-release-candidate-2.aspx