首页 > 技术文章 > C#在WebForm中的一般处理程序中使用Session

wang-min 2020-06-16 10:22 原文

转载于   https://www.cnblogs.com/gca123/p/6567371.html

 

.ashx中引用 session必须 using System.Web.SessionState ,继承IReadOnlySessionState/IRequiresSessionState

IReadOnlySessionState,为只读的session 不可以修改

IRequiresSessionState ,可以修改。

using System;
using System.Web;
using System.Text;
using System.Web.SessionState; //这是在.ashx 中引用session 的必备条件之一

 public class GetCheckData : IHttpHandler,IRequiresSessionState //继承IRequiresSessionState
 {

   public void ProcessRequest(HttpContext context)
    {        
       context.Response.ContentType = "text/plain";

       string ds="1234";

       context.Session["ds"] = ds;//这只是简单的使用方法,可以根据自己的要求改

       HttpContext.Current.Session["ds"]=ds;//第二种方法

       context.Response.Write(ds);

    }   

}

 

推荐阅读