Vary BY Param caching when we have ASP.NET routing values ​​instead of QueryString values

Usually, we can cache and establish a dependency on Request.QueryString values, for example

<%@ OutputCache Duration="15" VaryByParam="search" %> 

URLs for these may be as follows:

 http://www.demo.com/default.aspx?search=name 

But in my application, I use ASP.NET 4.0 routing, where I pass the product identifier, for example:

  http://www.demo.com/searchdetails/40563 

or

 http://www.demo.com/searchdetails/40564 

etc.

In this case, I get the product id somehow like

  Page.Route.Value["product_id"] 

In this case, how should I make the page dependent on this route value.

I'm new to caching, so I don't have much knowledge about this.

Do I need to do some kind of custom caching.

+2
source share
1 answer

I think you need to use VaryByCustom. Something like that:

 <%@ OutputCache Duration="15" VaryByParam="None" VaryByCustom="productIdInUrl" %> 

Then add your custom filter to the global.asax file:

 public override string GetVaryByCustomString(HttpContext context, string arg) { if(arg == "productIdInUrl") { return context.Request.RawUrl; } return base.GetVaryByCustomString(context, arg); } 

This will be different depending on your URL, not just productId. I think you could do another job on the Request object to do something smarter if needed

+2
source

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


All Articles