繁体   English   中英

输出缓存按URL(路由)ASP.NET 4

[英]Output Caching By Url (Route) ASP.NET 4

我没有找到明确的答案,有人可以帮助我吗?

如果我们有这样的网址

 www.website.com/results.aspx?listingtype=2&propertytype=1&location=alaska

然后我们可以设置

 <%@ OutputCache Duration="120" VaryByParam="listingtype;propertytype;location" %>

但我使用路由,所以我的网址看起来像这样:

 www.website.com/buy/houses/alaska

或者例如

 www.website.com/rent/condominiums/nevada

如何在VaryByParam中使用RouteValues,还是可以从代码隐藏或如何设置它? 我没有使用MVC,这是一个ASP.NET网站

编辑:(对于非ASP.NET MVC应用程序)

这个怎么样:

将OutputCache定义为:


<%@ OutputCache Duration="120" VaryByParam="None" VaryByCustom="listingtype;propertytype;location" %>

在Global.asax.cs中添加以下方法:


public override string GetVaryByCustomString(HttpContext context, string custom)
{
    if (custom == "lisingtype")
    {
        return GetParamFromRouteData("listingtype", context);
    }

    if (custom == "propertytype")
    {
        return GetParamFromRouteData("propertytype", context);
    }

    if (custom == "location")
    {
        return GetParamFromRouteData("location", context);
    }

    return base.GetVaryByCustomString(context, custom);
}

private string GetParamFromRouteData(string routeDataKey, HttpContext context)
{
    object value;

    if (!context.Request.RequestContext.RouteData.Values.TryGetValue(routeDataKey, out value))
    {
        return null;
    }

    return value.ToString();
}

旧内容:

如果您只是将OutputCache放在您的操作方法上,并将所有路径部分作为操作方法的一部分,则如下所示:


[OutputCache]
public ActionResult FindProperties(string listingtype, string propertytype, string location)
{
}

框架将自动为您更改这些项目的缓存(请参阅: http//aspalliance.com/2035_Announcing_ASPNET_MVC_3_Release_Candidate_2_.4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM