如何在有多个参数的情况下使用VaryByParam?

112

在ASP.NET MVC2中,我使用OutputCacheVaryByParam属性。当我有多个参数的方法时,我已经成功使用单个参数使其正常工作,但是正确的语法是什么?

[OutputCache(Duration=30, VaryByParam = "customerId"]
public ActionResult Index(int customerId)
{
//I've got this one under control, since it only has one parameter
}

[OutputCache(Duration=30, VaryByParam = "customerId"]
public ActionResult Index(int customerId, int languageId)
{
//What is the correct syntax for VaryByParam now that I have a second parameter?
}

我该如何让它同时使用这两个参数进行缓存?我需要将属性添加两次吗?还是将"customerId, languageId"作为值写入?

2个回答

216

VaryByParam 的有效值包括以下内容:

  • 字面字符串*(星号),它会根据操作方法的所有参数而变化。
  • 字面字符串none(不区分大小写),它不会根据操作方法的任何参数而变化。
  • 包含以分号分隔的您希望根据其变化的参数名称的字符串。

在您的情况下,您需要选择第一种选项:

[OutputCache(Duration = 30, VaryByParam = "*")]
public ActionResult Index(int customerId, int languageId)
{
}

然而,如果您有一些参数需要变化,而有些则不需要,那么您将使用第三个选项:

[OutputCache(Duration = 30, VaryByParam = "customerId;languageId")] // foo is omitted
public ActionResult Index(int customerId, int languageId, int foo)
{
}

参考文献。


1
您还可以使用*来包含所有参数。
 [OutputCache(Duration =9234556,VaryByParam = "*")]

2
欢迎来到 Stack Overflow。虽然这段代码可能回答了问题,但提供关于为什么和/或如何回答问题的额外上下文可以提高其长期价值。如何回答问题 - Elletlar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接