Ajax如何使用Session

时间:2023-03-09 22:02:56
Ajax如何使用Session

在Ajax中有时会使用到Session,在aspx.cs文件这样获取:

string name = Session["name"];

但是在Ajax中就不能这样获取Session,必须实现Session接口,如下

<%@ WebHandler Language="C#" class="msg" %>

using System;
using System.Web;
using Model;
using service;

public class msg : IHttpHandler,System.Web.SessionState.IRequiresSessionState  {//红色的就是实现Session接口
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

string vali_code =context.Session["vcode"].ToString().Trim(); 
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}