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);
}
精彩评论