我在app_code文件夹中有一个自定义的httphandler,我想在这个类中使用session但是有一条异常消息,这里是代码
public void ProcessRequest(HttpContext context)
{
HttpRequest request = context.Request;
HttpResponse response = context.Response;
HttpContext.Current.Session["UserID"] = "ABC";
response.Write(HttpContext.Current.Session["UserID"].ToString());
}
错误消息:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
任何人都知道问题是什么>
答案 0 :(得分:2)
您需要使处理程序实现IReadOnlySessionState
或IRequiresSessionState
(用于写访问)。
答案 1 :(得分:1)
如果要在HttpHandler中启用会话状态,则应从标记接口IRequiresSessionState继承处理程序
using System.Web.SessionState;
public class handler: IHttpHandler, IRequiresSessionState
{
}
答案 2 :(得分:1)