开发者

Basing ASP.NET Outputcache on querystring parameter AND session

I would like to know if its possible to use outputcache with a querystring parameter AND a session parameter together.

I'm serving location based content and the countryid is stored in a session, while other parameters as cate开发者_JS百科goryid, pageindex are stored in querystring.


It's possible to vary the Output Caching based on pretty much anything you want by using VaryByCustom, and providing a special function that will return the cache key string. For your case, try a directive like this:

<%@ OutputCache Duration="30" VaryByParam="myParam" VaryByCustom="mySessionVar" %>

Then in Global.asax, override the GetVaryByCustomString function for your application:

public override string GetVaryByCustomString(HttpContext context, string arg)
{
    if(arg == "mySessionVar" && Session["mySessionVar"] != null)
    {
        return Session["mySessionVar"].ToString();
    }

     return base.GetVaryByCustomString(context, arg);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜