2013-01-09 46 views
0

我在App_Code文件夹已经自定义的HttpHandler使用会话,我想在这个类中使用会话,但有一个例外的消息,这里是代码asp.net中自定义的HttpHandler

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

错误发生在哪里? – SLaks

回答

1

如果你想在eanable的HttpHandler会话状态,你应该继承标记接口处理器​​

using System.Web.SessionState; 

    public class handler: IHttpHandler, IRequiresSessionState 
    { 

    } 
+0

添加后:编译器错误消息:CS0246:无法找到类型或名称空间名称'IRequiresSessionState'(缺少使用指令或程序集引用吗?),缺少使用语句? – hkguile

+0

@hkinterview using System.Web.SessionState; ? – adt

2

您需要让您的处理程序实现IReadOnlySessionStateIRequiresSessionState(用于写入访问)。

1

您需要实施IReadOnlySessionState才能够从HttpHandler访问会话。

Here's一个很好的例子。

请注意,没有实现的方法,只需让您的处理程序实现接口即可。

相关问题