Session值的存储与删除

        private static void SetSession<T>(string key, T val)
        {
            HttpContext.Current.Session[key] = (T)val;
        }

        private static void RemoveSession(string key)
        {

            HttpContext.Current.Session.Remove(key);
        }

        private static T GetSession<T>(string key, T def)
        {

            var v = HttpContext.Current.Session[key];
            if (v != null)
            {
                return (T)v;
            }
            return default(T);
        }



        private static string SmsMobileKey
        {
            get
            {
                return "_smMobile";
            }
        }

        public static string SmsMobile
        {
            get
            {
                return GetSession<string>(SmsMobileKey, "");
            }
            set
            {
                if (value == (default(string)))
                {
                    RemoveSession(SmsMobileKey);
                }
                else
                {
                    SetSession(SmsMobileKey, value);
                }
            }
        }

 

posted @ 2016-02-27 22:06  BloggerSb  阅读(607)  评论(0)    收藏  举报