Mit Hilfe dieser statischen Klasse können Culture und UICulture typsicher in der ASP.NET Session gespeichert werden. Es wird das Facade-Pattern verwendet um den Zugriff auf die Session zu kapseln.
Anwendung:
Culture speichern:
SessionFacade.Culture = new CultureInfo(„de-DE“);
SessionFacade.UICulture = new CultureInfo(„de-DE“);
Culture auslesen:Thread.CurrentThread.CurrentCulture = SessionFacade.Culture;
Thread.CurrentThread.CurrentUICulture = SessionFacade.UICulture;
Es werden folgende Namespaces benötigt:
using System.Globalization;
using System.Threading;
using System.Globalization;
using System.Web;
/// <summary>
/// Facade pattern to save Culture and UICulture in current session
/// </summary>
public static class SessionFacade
{
# region Private Constants
private const string culture = "Culture";
private const string uiCulture = "UICulture";
# endregion
# region Public Properties
/// <summary>
/// Gets or sets the UIculture.
/// </summary>
/// <value>The UI culture.</value>
public static CultureInfo UICulture
{
set { HttpContext.Current.Session[uiCulture] = value; }
get
{
if (HttpContext.Current.Session[uiCulture] != null)
return (CultureInfo) HttpContext.Current.Session[uiCulture];
return null;
}
}
/// <summary>
/// Gets or sets the culture.
/// </summary>
/// <value>The culture.</value>
public static CultureInfo Culture
{
set { HttpContext.Current.Session[culture] = value; }
get
{
if (HttpContext.Current.Session[culture] != null)
return (CultureInfo) HttpContext.Current.Session[culture];
return null;
}
}
# endregion
}
Kommentare zum Snippet