OutputCache VaryByParam changes by a parameter that is not supposed to be included

I am using OutputCache in MVC 5 to cache views on a server.

I only want to cache the view based on two parameters in the query string.

Action method

[HttpGet] [OutputCache(Location = OutputCacheLocation.Server, Duration = 60*10, VaryByParam = "id;quoteid")] public ActionResult MyAction(int id, ProductCategoryType category) { return Content(DateTime.Now.ToString()); } 

Route

 context.MapRoute( "MyCustomRoute", "myarea/{controller}/{action}/{id}/{category}/{name}/{quoteId}", new { controller = "MyController", name = UrlParameter.Optional, quoteId = UrlParameter.Optional }, new[] { "MyNamespace.Areas.MyArea.Controllers" }); 

URL

 http://localhost:17191/myarea/mycontroller/myaction/2/1/a-holiday/aquoteid 

This works, but correctly binds the data , however, if I change any part of the {name} URL, it still generates a new cache element, although in my action method I have VaryByParam="id;quoteid"

For instance...

 http://localhost:17191/myarea/mycontroller/myaction/2/1/a-holiday/somequoteid 

and

 http://localhost:17191/myarea/mycontroller/myaction/2/1/another-holiday/somequoteid 

... generate different DateTime outputs, but they shouldn't - they should be identical.

What have I done wrong and how can I achieve the desired behavior?


Edit

To be clear, ProductCategoryType is the Enum associated with an int value. The binding for this is correct when I debug an ActionResult

Edit 2 Since I was asked to show ProductCategoryType , I added it below. It truly ties together when I debug it - I don't think it has anything to do with the problem.

 public enum ProductCategoryType { TourActivity = 1, Accommodation = 2, BusPass = 3, SelfDrive = 4, } 

Edit 3

URL change: http://localhost:17191/a/products/view/2/1?name=test1"eid=123

And now the cache works as expected, but how can I achieve this with a prettier URL through routing?

+6
source share
1 answer

The mechanism using VaryByParam acts specifically on the request or message parameters of the actual raw HTTP request and is not aware of any URLs that map this raw request to any other form. Thus, in your case, it will not see the id or quoteid parameters at all (since they are not actually executed in querystring or post).

However, he will notice that the URL itself (before any "?", Including the name) is also cached.

You may need to use VaryByCustom. See here for an example. And here is a very similar example of an SO question here .

+2
source

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


All Articles