views:

157

answers:

1

If I have a page with outputcaching (let's call it Employees.aspx) which accepts one parameter (through querystring) called Company like this:

http://www.example.com/Employees.aspx?Company=Google

How can I avoid duplicate page cache entries for differently cased URLs like:

http://www.example.com/Employees.aspx?Company=GOOGLE
http://www.example.com/Employees.aspx?Company=GOoGlE

I've enabled output caching through the OuputCaching directive as follows:

<%@ OutputCache Duration="300" VaryByParam="Company"  %>

Is there any way to programatically set what the "unique cache key" for this particular request should be

+3  A: 

One hack-esque method of doing it would be to do a VaryByCustom (instead of VaryByParam) and do the .ToLower/.ToUpper in there.

Change the OutputCache directive to something like this:

<%@ OutputCache Duration="300" VaryByCustom="Company" VaryByParam="none" %>

...and add an override in Global.asax.cs for GetVaryByCustomString:

public override string GetVaryByCustomString(System.Web.HttpContext context, string custom)
{
    string CustomValue = "";
    switch (custom.ToLower())
    {
        case "company":
            CustomValue = context.Request.QueryString["company"] ?? "";
            CustomValue = CustomValue.ToLower();
            break;
    }
    return CustomValue;
}
patridge