Generic Session Helper

 public static class AllSession
    {

        public static string Get(string key)
        {
            object value = HttpContext.Current.Request.QueryString[key];
            return (value == null) ? null : value.ToString();
        }

        public static string Post(string key)
        {
            object value = HttpContext.Current.Request.Form[key];
            return (value == null) ? null : value.ToString();
        }

        public static string Session(string key)
        {
            object value = HttpContext.Current.Session[key];
            return (value == null) ? null : value.ToString().Trim();
        }

        public static void ClearSession(string key)
        {
            HttpContext.Current.Session[key] = null;
        }

        public static void StoreInSession(string key, object value)
        {
            HttpContext.Current.Session[key] = value;
        }

        public static long TownID
        {
            get
            {

                return GetFromSession<long>(“TownID”);
            }
            set
            {
                SetInSession<long>(“TownID”, value);
            }
        }

        public static string TownName
        {
            get
            {

                return GetFromSession<string>(“TownName”);
            }
            set
            {
                SetInSession<string>(“TownName”, value);
            }
        }
        public static string UnitTime
        {
            get
            {

                return GetFromSession<string>(“UnitTime”);
            }
            set
            {
                SetInSession<string>(“UnitTime”, value);
            }
        }
        public static long UserId
        {
            get
            {

                return GetFromSession<long>(“UserId”);
            }
            set
            {
                SetInSession<long>(“UserId”, value);
            }
        }

        public static void Abandon()
        {
            if (HttpContext.Current != null && HttpContext.Current.Session != null)
                HttpContext.Current.Session.Abandon();
        }

        private static T GetFromSession<T>(string key)
        {
            if (HttpContext.Current.Session == null)
            {
                return default(T);
            }
            object obj = HttpContext.Current.Session[key];
            if (obj == null)
            {
                return default(T);
            }
            return (T)obj;
        }

        private static void SetInSession<T>(string key, T value)
        {
            if (value == null)
            {
                HttpContext.Current.Session.Remove(key);
            }
            else
            {
                HttpContext.Current.Session[key] = value;
            }
        }

        private static T GetFromApplication<T>(string key)
        {
            return (T)HttpContext.Current.Application[key];
        }

        private static void SetInApplication<T>(string key, T value)
        {
            if (value == null)
            {
                HttpContext.Current.Application.Remove(key);
            }
            else
            {
                HttpContext.Current.Application[key] = value;
            }
        }
    }

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s