This project is read-only.

Request Personalization Options Made Easy

In many web applications at one moment you'll need to store some temporary info about your user. It might be the theme they chose, the interface language or how many posts should be displayed on a page.
Suppose your application has supports themes that can be changed by adding "?theme=theme_name" to url. The theme remains until changed again.
The usual steps for handling that are:
  1. get the theme and cache it in a cookie
  2. if not url parameter is found, check for cookie

Let's do that
var themeParam = new RequestPersonalizationParameter<string>("theme");
//try to load from request url			
if (themeParam.LoadFromString(Request.QueryString["theme"]))
{
    //cache it in a cookie with name "_theme"
    themeParam.Cache();				
}
else
{
        //try to load from cached cookie
	themeParam.LoadFromCache();
}
//if no theme was requested nor cached, use the "default" theme.
var theme = themeParam.Value ?? "default";


RequestLocale Helper

This class will make it easy for you to find out what locale was requested by the user when supporting multiple languages. It's a derivation of "RequestPersonalizationParameter" class. Note that it uses CultureInfo for locale value, instead of a simple string.
var localeParam = new RequestLocale();
//try to load from request url			
if (localeParam.LoadFromString(Request.QueryString["locale"]))
{
    //cache it in a cookie with name "_locale"
    localeParam.Cache();				
}
else
{
        //try to load from cached cookie
	localeParam.LoadFromCache();
}
//if no locale was requested nor cached, use the invariant (english) locale
var locale = localeParam.Value ??CultureInfo.InvariantCulture;

Last edited Jan 21, 2011 at 1:43 PM by mike_sapiens, version 3

Comments

No comments yet.