ASP.NET MVC Caching vary by controller action parameter
Asked Answered
L

2

3

Is there any way I can vary caching by a controller action parameter using the outputcache attribute? We have varybyparam which will not work if my parameters are embedded within the URL in a REST manner.

Lazarolazaruk answered 11/12, 2008 at 22:48 Comment(0)
I
6

Caching works this way by default. Different URLs give different cache locations. Perhaps there is something missing from your question, but, as stated, it already works this way.

Indemnity answered 12/12, 2008 at 13:54 Comment(0)
V
10

Its also important to realize in a Action method that 'VaryByParam' does not mean 'Vary by the parameters being passed into that action method'. it means 'vary by the parameters being passed into that action method that originate as HTTP parameters'.

[OutputCache(CacheProfile = "ContentPage", VaryByParam = "mode")]
public ActionResult Index(string key, string mode)
{

}

Lets say the route for this action method is :

  routes.MapRoute(
   "video-route-short",
   "video/{key}",
   new { controller = "Video", action = "Index", key = (string)null }
  );

As Craig says above, the key parameter is part of the URL and therefore caching doesn't apply to it - so it is essentially always cached.

The mode parameter which would be sent via a '?mode=1' type would apply to the caching.

Vanhouten answered 14/5, 2009 at 18:39 Comment(1)
As of MVC3 this is no longer the case seeCabob
I
6

Caching works this way by default. Different URLs give different cache locations. Perhaps there is something missing from your question, but, as stated, it already works this way.

Indemnity answered 12/12, 2008 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.