Global.asax Override and Handling Different Browser Renderings

Posted by Mike Iskiw on July 13, 2011
0 people like this tutorial

Global.asax Override

In the below code we show an override to the GetVaryByCustomString method within the Global.asax file where we can append a custom string to the output cache. In the below example we are get the Default Agility Output Cache key with “GetAgilityVaryByCustomString(context)” and then we append the currently logged in UserID that is set in a custom UserProfileContext class.
public override string GetVaryByCustomString(HttpContext context, string custom)
        if (string.Compare(custom, "AgilityCacheControl", true) == 0)
            StringBuilder sb = new StringBuilder();
            return sb.ToString();

        return base.GetVaryByCustomString(context, custom);
Now with the above code we are able to generate different output cache results for each user of the website which will remove the issue with the Profile.aspx page loading incorrect user content. An example cacheKey would for UserID= 123 would be: {AgilityPage_For_Profile_aspx}123 And for a user with UserID=456 would be: {AgilityPage_For_Profile_aspx}456 Therefore we are not loading the page and always getting: {AgilityPage_For_Profile_aspx} which would return the same page to each user.

Handling Different Browser Renderings

The example given above handling output cache for different users can also be applied for changes in the output based on the browser agent. This is useful if you need to have different page renderings based on a browser time (for example Desktop browsers vs. Mobile Browsers) but still have the same page name/code. You can get a handle on the users current Browser agent using the HttpContext Request.Browser property and append the browser agent string to the output cache string as seen previously.

Add a Comment



Log in with your Agility username and password or
register for the site

Log In