May 21st, 2015

A Better Way To Manage ASP.NET Session With Generic Wrapper

Before we get started, let us see the general way to use Session in ASP.NET

if (Session["UserName"] == null) 
{ 
    LabelUserName.Text = "Anonymous"; 
} 
else 
{ 
    LabelUserName.Text = (string)Session["UserName"]; 
} 

You can feel some drawbacks e.g. to check null reference, type safety, to cast object..etc.

Now add following class and see how it solves the issues: