1

http://support.microsoft.com/kb/2527105上的代码示例正是我在两个子域之间共享会话所需要的。唯一的问题是它在现实生活中不起作用。当请求的唯一文件是页面本身时,它工作正常,但是当其他文件是请求的一部分时抛出错误“会话状态在此上下文中不可用”,例如如果我向页面添加样式表或 javascript 文件。代码在下面的“if (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 = "/";
    }
}
4

1 回答 1

1

您可以尝试一个 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;
        }
    }
于 2012-03-19T16:37:56.503 回答