天天看點

在handler中讀取session值

最近做一個項目,有使用Ajax調用ashx檔案 ,其中ashx檔案裡面有用到Session,但是總無法擷取Session,經過調試,出現的錯誤的原因: 大體如下 Session["loginName"]為空,無法toString(); context.Session["Id"] “context.Session”引發了“System.NullReferenceException”類型的異常 base {System.SystemException}: {"該方法的指針為空。"} 查閱MSDN得到ashx檔案要使用Session,

必須實作Session接口;

using System;

using System.Web;

using System.Web.SessionState; //第一步:導入此命名空間

public class checkCookie : IHttpHandler ,IRequiresSessionState //第二步:實作接口 到此就可以像平時一樣用Session了 { public void ProcessRequest (HttpContext context)

注意:

1、如果是要在HttpHandler 中讀取Session的内容,就要在實作IHttpHandler 的類中同時實作IReadOnlySessionState 這個接口。

2、如果是要在HttpHandler 中讀寫Session的内容,就要在實作IHttpHandler 的類中同時實作IRequiresSessionState