我在同一个域中有一个 aspx 页面和自定义 httphandler。在 aspx 页面(test.aspx)中,我使用
<%@ Page Language="C#" %>
<%
HttpContext.Current.Session["UserID"] = "ABC";
%>
创建一个会话变量,但是当我想在 httphandler 中调用该变量时
public class JpgHttpHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
response.Write(HttpContext.Current.Session["UserID"].ToString());
}
}
当我切换 httphandler 时出现错误消息:
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.
我如何从 httphandler 调用会话?
谢谢
更新
将代码更改为
if (context.Session["UserID"] != null)
response.Write(context.Session["UserID"].ToString());
奇怪的是,当我使用 ip 访问网页时,它可以工作。但是我使用域名访问页面,它显示为空白