Home > ASP.NET > How to create session variable which allows you to access your session properties in a type-safe way from any class

How to create session variable which allows you to access your session properties in a type-safe way from any class

public class SessionHandler
{
private static string _userid;
private static string _menuid;

private SessionHandler() { }

public static SessionHandler Current
{
get
{
SessionHandler session =
(SessionHandler)HttpContext.Current.Session[“__MySession__”];
if (session == null)
{
session = new SessionHandler();
HttpContext.Current.Session[“__MySession__”] = session;
}
return session;
}
}

public string userid
{
get { return _userid; }
set { _userid = value; }

}
}

access values of session variables like this

SessionHandler.Current.userid

Categories: ASP.NET Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment