ASP.NET Outputcache basics for querystring AND session

I would like to know if it is possible to use outputcache with the querystring parameter and the session parameter.

I serve location-based content and the countryid is stored in the session, while other parameters like categoryid, index_index are stored in querystring.

+3
source share
1 answer

You can change the output caching based on almost everything you want using VaryByCustom and providing a special function that will return a cache key string. For your case, try this directive:

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

Then in Global.asax redefine 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);
}
-1
source

Source: https://habr.com/ru/post/1735293/


All Articles