asp.net 在ashx页面使用session存储

在平常的页面上很容易就的到request,response对像,从而对其进行一些操作,但在ashx(一般处理程序)中却是有一点的不同,

在ashx你无法正常的使用session,即

1 context.session["TestA"]="1111";

虽然生成的时候是不报错的,但是是赋值不了的,而且也取不到值.

如果要在ashx中使用session,需要引用   using System.Web.SessionState;  并且实现 IRequiresSessionState 接口.

using System.Web.SessionState;
public class login : IHttpHandler, IRequiresSessionState {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string type = context.Request.Form["type"];
        switch (type)
        {
            case "0":
                sendMessage(context);
                break;
            case "1":
                saveUserRealInfo(context);
                break;
        }
}
 /*用户发送短信*/
    private void sendMessage(HttpContext context)
    {
        string mobile = context.Request.Form["mobile"];
        if (Regex.IsMatch(mobile,@"^1[3-9]d{9}$"))
        {
           // string  data = CommClass.Comm.sendMessage(mobile, 1);
            string  data ="5668";
            if (data!= "")
            {
/*通过session存储验证码,方便页面验证*/ context.Session[mobile]=data; context.Response.Write("发送成功!"); ------------------

  

原文地址:https://www.cnblogs.com/fogwang/p/11404913.html