想在在ashx中读写session需要实现System.Web.SessionState.IRequiresSessionState接口
public class login : IHttpHandler, IRequiresSessionState { public string code = string.Empty; public string passWord = string.Empty; public string name = string.Empty; public string role = string.Empty; public string Method = string.Empty; DataTable dt = new DataTable(); public void ProcessRequest(HttpContext context) { string re = string.Empty; context.Response.ContentType = "text/plain"; code = HttpContext.Current.Request["code"]; passWord = HttpContext.Current.Request["passWord"]; dt = Exam.login(code, passWord); name = Convert.ToString(dt.Rows[0][0]); role = Convert.ToString(dt.Rows[0][2]); re = Convert.ToString(dt.Rows[0][3]); if (re.Equals("登录成功")) { context.Session["code"] = code; context.Session["name"] = name; context.Session["role"] = role; HttpContext.Current.Response.Redirect("/aspx/main.aspx"); } }