ASP.Net MVC Output Caching: The directive or the configuration settings profile must specify the 'varyByParam' attribute
Asked Answered
F

1

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?

Filipino answered 13/11, 2008 at 22:41 Comment(0)
F
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

Filipino answered 18/11, 2008 at 0:31 Comment(5)
Not an empty string. According to MSDN, if no parameters are to be used, the value of VaryByParam should be "none": msdn.microsoft.com/en-us/library/…Supplicate
Ah, that's useful to know - I guess that's the problem with good ol' magic strings eh?Filipino
Crazy stuff - what if you have a url param called 'none'?Minyan
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"); }Reparative
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() { ... }Filipino

© 2022 - 2024 — McMap. All rights reserved.