112

In ASP.NET MVC2 I use OutputCache and the VaryByParam attribute. I got it working fine with a single parameter, but what is the correct syntax when I have several parameters on the method?

[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?
}

How do I get it to cache the pages using both parameters? Do I enter add the attribute twice? Or write "customerId, languageId" as the value??

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Frode Lillerud
  • 7,324
  • 17
  • 58
  • 69

2 Answers2

216

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.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Kevin LaBranche
  • 20,908
  • 5
  • 52
  • 76
1

You can also use * to include all parameters

 [OutputCache(Duration =9234556,VaryByParam = "*")]
Aryan Firouzian
  • 1,940
  • 5
  • 27
  • 41
pooja gautam
  • 164
  • 2
  • 2
  • 2
    Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. [How to Answer](https://stackoverflow.com/help/how-to-answer) – Elletlar Feb 08 '19 at 09:37