14

I encountered the above error message after applying the OutputCache attribute on ActionResult methods with no input parameters - why would I use VaryByParams in this case? Is this a bug in ASP.Net MVC or is there a way of using OutputCache without setting this property?

My big question is, if I have to use VaryByParams, what should I enter for a value if I have no parameters to cache against?

Zac Seth
  • 2,742
  • 5
  • 37
  • 56

1 Answers1

26

I never found a satisfactory answer for this - basically, you just need to add the VaryByParams property and if you don't have any parameters set it to the magic string "none".

http://msdn.microsoft.com/en-us/library/system.web.ui.outputcacheparameters.varybyparam.aspx

Portman
  • 31,785
  • 25
  • 82
  • 101
Zac Seth
  • 2,742
  • 5
  • 37
  • 56
  • 10
    Not an empty string. According to MSDN, if no parameters are to be used, the value of VaryByParam should be "none": http://msdn.microsoft.com/en-us/library/system.web.ui.outputcacheparameters.varybyparam.aspx – bzlm Feb 25 '09 at 21:42
  • 3
    Ah, that's useful to know - I guess that's the problem with good ol' magic strings eh? – Zac Seth Mar 03 '09 at 23:18
  • 3
    Crazy stuff - what if you have a url param called 'none'? – DavidWainwright Feb 20 '13 at 15:56
  • where to specify this attribute and how?My Code is as follows and i am getting the same error:- [OutputCache(Duration=10)] public String Index(OutputCacheAttribute none) { return DateTime.Now.ToString("T"); } – F11 Apr 23 '13 at 11:11
  • 1
    If you're using ASP.Net MVC then it looks like your action method signature isn't valid - you should be returning some sort of `ActionResult` I believe (as opposed to `String` which you're using in your comment). Also, you shouldn't pass `OutputCacheAttribute none` as a parameter - it's a C# Attribute and you're already using it when you prepend it to the start of the method (`[OutputCache...`). So, your action method should look like this: `[OutputCache(Duration=10, VaryByParams="none")] public ActionResult Index() { ... }` – Zac Seth Apr 25 '13 at 00:09