VaryByParam = "*" also reads RouteData.Values?

in my asp.net mvc project, I enable output caching on the controller as shown below

[OutputCache(Duration = 100, VaryByParam = "*", VaryByHeader = "X-Requested-With")] public class CatalogController : BaseController { public ActionResult Index(string seller) { // I do something } } 

it works fine until my own Route class is created as shown below

 public class MyRoute : Route { // there is a constructor here.. // I override this method.. // just to add one data called 'seller' to RouteData public override RouteData GetRouteData(HttpContextBase httpContext) { var data = base.GetRouteData(httpContext); if (data == null) return null; var seller = DoSomeMagicHere(); // add seller data.Values.Add("seller", seller); return data; } } 

and then the action method will accept seller as a parameter. I tested it, always providing different parameters to seller , but it takes cache output instead of calling a method.

Does the setting VaryByParam = "*" also depend on the RouteData.Values ​​parameter in asp.net mvc?

I am using ASP.Net 4 MVC 3 RC 2

+5
source share
2 answers

The mechanism for caching output depends on the URL, QueryString, and Form. The parameters of RouteData.Values ​​are not presented here. The reason for this is because the output cache module works before routing, so when the second request arrives and the output cache module looks for a suitable cache entry, it does not even have a RouteData object to check.

This is usually not a problem, because RouteData.Values ​​comes directly from a URL that is already accounted for. If you want to change your value, use VaryByCustom and GetVaryByCustomString to accomplish this.

+7
source

If you remove VaryByParam = "*", it should use the values ​​of the action method parameter when caching.

The ASP.NET MVC 3s output caching system no longer requires you to specify the VaryByParam property when declaring the [OutputCache] attribute in the controller action method. MVC3 now automatically changes the output cached entries when you have explicit parameters for your action method - allows you to purely allow output ...

Source: http://weblogs.asp.net/scottgu/archive/2010/12/10/announcing-asp-net-mvc-3-release-candidate-2.aspx

 [OutputCache(Duration = 100, VaryByHeader = "X-Requested-With")] public class CatalogController : BaseController { public ActionResult Index(string seller) { // I do something } } 
+3
source

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


All Articles