ASP.NET MVC caching depends on controller action parameter

Is it possible to somehow change the caching using the controller action parameter using the outputcache attribute? We have varybyparam that will not work if my parameters are embedded in the url in REST mode.

+2
source share
2 answers

Caching works by default. Different URLs provide different cache locations. Maybe something is missing in your question, but as said, it already works that way.

+5
source

It is also important to implement in the Action method that "VaryByParam" does not mean "Changed by the parameters passed to this action method." this means that "depends on the parameters passed to this action method that come from the HTTP parameters."

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

Assume the route for this action method:

  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 the cache does not apply to it - therefore, it is essentially always cached.

The mode parameter that will be sent using the type ' ?mode=1 Mode ?mode=1 , applies to caching.

+10
source

Source: https://habr.com/ru/post/987050/


All Articles