标签:
我想在ashx.cs中或者在class.cs中使用session,但是发现总是出现
“未将引用设置到实例的错误”
最后发现IHttpHandler,IRequiresSessionState 并不能对session进行修改
最后加了这些才可以,实现了对session的读取与修改。
最好把session和cookie方法封装成类
CookieHelper:
/* 源码己托管:http://git.oschina.net/kuiyu/dotnetcodes */ using System; using System.Web; namespace DotNet.Utilities { public class CookieHelper { /// <summary> /// 清除指定Cookie /// </summary> /// <param name="cookiename">cookiename</param> public static void ClearCookie(string cookiename) { HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename]; if (cookie != null) { cookie.Expires = DateTime.Now.AddYears(-3); HttpContext.Current.Response.Cookies.Add(cookie); } } /// <summary> /// 获取指定Cookie值 /// </summary> /// <param name="cookiename">cookiename</param> /// <returns></returns> public static string GetCookieValue(string cookiename) { HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename]; string str = string.Empty; if (cookie != null) { str =HttpUtility.UrlDecode(cookie.Value); } return str; } /// <summary> /// 添加一个Cookie(24小时过期) /// </summary> /// <param name="cookiename"></param> /// <param name="cookievalue"></param> public static void SetCookie(string cookiename, string cookievalue) { SetCookie(cookiename, cookievalue, DateTime.Now.AddDays(1.0)); } /// <summary> /// 添加一个Cookie /// </summary> /// <param name="cookiename">cookie名</param> /// <param name="cookievalue">cookie值</param> /// <param name="expires">过期时间 DateTime</param> public static void SetCookie(string cookiename, string cookievalue, DateTime expires) { HttpCookie cookie = new HttpCookie(cookiename) { Value = cookievalue, Expires = expires }; HttpContext.Current.Response.Cookies.Add(cookie); } } }
SessionHelper:
/* 源码己托管:http://git.oschina.net/kuiyu/dotnetcodes */ using System.Web; namespace DotNet.Utilities { public static class SessionHelper2 { /// <summary> /// 添加Session,调动有效期为20分钟 /// </summary> /// <param name="strSessionName">Session对象名称</param> /// <param name="strValue">Session值</param> public static void Add(string strSessionName, string strValue) { HttpContext.Current.Session[strSessionName] = strValue; HttpContext.Current.Session.Timeout = 20; } /// <summary> /// 添加Session,调动有效期为20分钟 /// </summary> /// <param name="strSessionName">Session对象名称</param> /// <param name="strValues">Session值数组</param> public static void Adds(string strSessionName, string[] strValues) { HttpContext.Current.Session[strSessionName] = strValues; HttpContext.Current.Session.Timeout = 20; } /// <summary> /// 添加Session /// </summary> /// <param name="strSessionName">Session对象名称</param> /// <param name="strValue">Session值</param> /// <param name="iExpires">调动有效期(分钟)</param> public static void Add(string strSessionName, string strValue, int iExpires) { HttpContext.Current.Session[strSessionName] = strValue; HttpContext.Current.Session.Timeout = iExpires; } /// <summary> /// 添加Session /// </summary> /// <param name="strSessionName">Session对象名称</param> /// <param name="strValues">Session值数组</param> /// <param name="iExpires">调动有效期(分钟)</param> public static void Adds(string strSessionName, string[] strValues, int iExpires) { HttpContext.Current.Session[strSessionName] = strValues; HttpContext.Current.Session.Timeout = iExpires; } /// <summary> /// 读取某个Session对象值 /// </summary> /// <param name="strSessionName">Session对象名称</param> /// <returns>Session对象值</returns> public static string Get(string strSessionName) { if (HttpContext.Current.Session[strSessionName] == null) { return null; } else { return HttpContext.Current.Session[strSessionName].ToString(); } } /// <summary> /// 读取某个Session对象值数组 /// </summary> /// <param name="strSessionName">Session对象名称</param> /// <returns>Session对象值数组</returns> public static string[] Gets(string strSessionName) { if (HttpContext.Current.Session[strSessionName] == null) { return null; } else { return (string[])HttpContext.Current.Session[strSessionName]; } } /// <summary> /// 删除某个Session对象 /// </summary> /// <param name="strSessionName">Session对象名称</param> public static void Del(string strSessionName) { HttpContext.Current.Session[strSessionName] = null; } } }
标签:
原文地址:http://www.cnblogs.com/ithuo/p/4882831.html