如何使用带有多个参数的 VaryByParam?

在 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,language ageId”作为值? ?

53580 次浏览

Valid values for VaryByParam are one of the following:

  • The literal string * (asterisk), which varies by all parameters of the action method.
  • The literal string none (case-insensitive), which varies by no parameters of the action method.
  • A string containing the semicolon-separated names of the parameters you wish to vary by.

In your case, you'd want the first option:

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

If, however, you had some params you want to vary by and some that you don't, then you'd use the third option:

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

Reference.

You can also use * to include all parameters

 [OutputCache(Duration =9234556,VaryByParam = "*")]