19 January 2014

Handling Session in Common Class in ASP.NET WebForms or ASP.NET MVC

Handling Session in Common Class in ASP.NET WebForms or  ASP.NET MVC

SessionVariables.cs
using System;
using System.Web;
namespace WebApplication1
{
    public class SessionVariables
    {

        ///
        ///  Store EmpID in Session
        ///
        public static Int64 EmpID
        {
            get
            {
                return (Int64)HttpContext.Current.Session["EmpID"];
            }

            set
            {
                HttpContext.Current.Session["EmpID"] = value;
            }
        }
       
       
        ///
        /// Store EmpName in Session
        ///
        public static string EmpName
        {
            get
            {
                return (string)HttpContext.Current.Session["EmpName"];
            }

            set
            {
                HttpContext.Current.Session["EmpName"] = value;
            }
        }
     
    }

}

You can get or set the session variables in Webforms or MVC

using System;
using System.Web.UI;

namespace WebApplication1
{
    public partial class HandlingSession : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SessionVariables.EmpID = 1;

            SessionVariables.EmpName = "ArunPrakash";
        }
    }
}

Advantages:
1. Reduces duplicate Session variables declaration
2. Type conversion is in common functions.

No comments:

Post a Comment

Comments Welcome

Consistency level in Azure cosmos db

 Consistency level in Azure cosmos db Azure Cosmos DB offers five well-defined consistency levels to provide developers with the flexibility...