簡體   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