C# 当我们使用ASP.NET路由值而不是QueryString值时,缓存会因参数而异
通常我们可以进行缓存,并依赖于Request.QueryString值,如C# 当我们使用ASP.NET路由值而不是QueryString值时,缓存会因参数而异,c#,asp.net,outputcache,C#,Asp.net,Outputcache,通常我们可以进行缓存,并依赖于Request.QueryString值,如 <%@ OutputCache Duration="15" VaryByParam="search" %> 但在我的应用程序中,我使用ASP.NET 4.0路由,其中我传递产品的id,如: http://www.demo.com/searchdetails/40563 或 等等 在本例中,我访问产品id,如下所示 Page.Route.Value["product_id"] 在这种情况下,我应该如何
<%@ OutputCache Duration="15" VaryByParam="search" %>
但在我的应用程序中,我使用ASP.NET 4.0路由,其中我传递产品的id,如:
http://www.demo.com/searchdetails/40563
或
等等
在本例中,我访问产品id,如下所示
Page.Route.Value["product_id"]
在这种情况下,我应该如何使页面依赖于此路由值
我是缓存新手,所以我对缓存没有太多的了解
我们是否需要做一些自定义缓存。我认为您必须使用VaryByCustom。 大概是这样的:
<%@ OutputCache Duration="15" VaryByParam="None" VaryByCustom="productIdInUrl" %>
这将在您的所有URL上有所不同,而不仅仅是productId。我想您可以在请求对象上做更多的工作,以便在需要时做一些更聪明的事情
Page.Route.Value["product_id"]
<%@ OutputCache Duration="15" VaryByParam="None" VaryByCustom="productIdInUrl" %>
public override string GetVaryByCustomString(HttpContext context,
string arg)
{
if(arg == "productIdInUrl")
{
return context.Request.RawUrl;
}
return base.GetVaryByCustomString(context, arg);
}