Caching Output by ASP.NET 4 URL (Route)

I did not find a clear answer to this question, so can someone help me?

If we have such a URL

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

Then we can set

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

But I use routing, so my url looks like this:

 www.website.com/buy/houses/alaska

or for example

 www.website.com/rent/condominiums/nevada

How to use RouteValues ​​in VaryByParam, or can I install it from code or how? I do not use MVC, this is an ASP.NET website

+3
source share
1 answer

Edit: (for ASP.NET MVC applications)

How about this:

Make the definition of OutputCache as follows:


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

In Global.asax.cs add the following methods:


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();
}

Old Content:

If you just put OutputCache in your action method and make all parts of your route part of your action, something like this:


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

(. http://aspalliance.com/2035_Announcing_ASPNET_MVC_3_Release_Candidate_2_.4)

+4

All Articles