Caching Vary BY Param when we have ASP.NET Routing Values instead of QueryString values

人盡茶涼 提交于 2019-12-12 18:33:43

问题


Normally we can do caching and make dependency on Request.QueryString values like

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

The url for such may be like:

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

But in my application i am using ASP.NET 4.0 routing where i am passing the id of a product like:

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

or

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

and so on.....

In this case i access the product id something as

 Page.Route.Value["product_id"]

In this case how should i make the dependency of page on this route value.

I am new to caching so i dont have any much knowledge of the same.

Do we need to do some custom caching.


回答1:


I think you have to use VaryByCustom. Something like this:

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

And 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 would vary on all of your URL, not just the productId. I guess you could do some more work on the Request object to do something more clever if needed



来源:https://stackoverflow.com/questions/15055994/caching-vary-by-param-when-we-have-asp-net-routing-values-instead-of-querystring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!