当请求中有多个文件时'会话状态在此上下文中不可用'
问题描述:
http://support.microsoft.com/kb/2527105的代码示例正是我需要在两个子域之间共享会话的代码示例。唯一的问题是它在现实生活中不起作用。当被请求的ony文件是页面本身时它工作正常,但当其他文件是请求的一部分时(例如,如果我将样式表或JavaScript文件添加到页面),会引发错误“会话状态在此上下文中不可用”。该代码在 “!如果(context.Session = NULL & &” 的行生成此错误:当请求中有多个文件时'会话状态在此上下文中不可用'
void context_PostRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication context = (HttpApplication)sender;
HttpCookie cookie = context.Response.Cookies["ASP.NET_SessionId"];
if (context.Session != null &&
!string.IsNullOrEmpty(context.Session.SessionID))
{
cookie.Value = context.Session.SessionID;
if (rootDomain != "localhost")
{
cookie.Domain = rootDomain;
}
cookie.Path = "/";
}
}
答
你可以尝试一个try catch块,那么您可以为空值陷阱
。private HttpSessionState GetSession(HttpApplication context)
{
try
{
//On Abadon or Logout this returns "Session state is not available in this context. "
return context.Session;
}
catch (HttpException)
{
return null;
}
}
你应该给这个标题描述问题,而不是询问人们是否熟悉这个例子 – mydogisbox 2012-02-22 17:16:46
好的,编辑标题,希望能吸引一些熟悉的人 – BeachBum 2012-02-22 17:31:04
我调整了一下 – mydogisbox 2012-02-22 17:46:16