开发者

Basing ASP.NET Outputcache on querystring parameter AND session

开发者 https://www.devze.com 2022-12-21 16:52 出处:网络
I would like to know if its possible to use outputcache with a querystring parameter AND a session parameter together.

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

精彩评论

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