3

私はこれに対する明確な答えを見つけられませんでした、それで誰かが私を助けることができますか?

このようなURLがある場合

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

次に設定できます

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

しかし、私はルーティングを使用しているので、私のURLは次のようになります。

 www.website.com/buy/houses/alaska

または例えば

 www.website.com/rent/condominiums/nevada

VaryByParamでRouteValuesを使用するにはどうすればよいですか、それともコードビハインドから設定できますか?私はMVCを使用していません。これはASP.NETWebサイトです。

4

1 に答える 1

4

編集:(非ASP.NETMVCアプリの場合)

これはどう:

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を参照) 。

于 2012-05-13T07:04:41.503 に答える