views:

91

answers:

3

I know that i can use the VaryByParam attribute, but what it does is not exactly what i am aiming for.

What happens is that different versions of the page are cached based on the query string.

For example Page.aspx?ID=Tom has its own cached version and ID=Arnold has another. Now what i want to do is not to cache Arnold at all. I only want to cache the page when the query string is Tom.

Any ideas?

A: 

Try to use VaryByCustom and override GetVaryByCustomString method in the Application class.

limpalex
A: 

Here is the detailed explanation.

From MSDN,

<%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="Tom" %>

In the Global.asax file,

<%@ Application language="C#" %>
<script runat="server">
public override string GetVaryByCustomString(HttpContext context, 
    string arg)
{
    if(arg == "Tom")
    {
        return "ID=" +
            context.Request.QueryString["ID"].ToString();
    }
    return base.GetVaryByCustomString(context, arg);
}
</script>
Srinivas Reddy Thatiparthy
is there another way, i don't like using the global.asax
diamandiev
AFAIK,there is no other way..
Srinivas Reddy Thatiparthy
A: 
    <Extension()> _
    Public Sub CacheOutput(ByVal Page As Page, ByVal Minutes As Integer)
        Page.Response.Cache.SetExpires(Date.Now.AddMinutes(Minutes))
        Page.Response.Cache.SetCacheability(HttpCacheability.Server)
        Page.Response.Cache.SetValidUntilExpires(True)
    End Sub

This works. Note that any version of the page that has a querystring or a POST parameter will not be cached.

diamandiev