为什么“文件不存在”。 Application_Error中的异常无法访问会话数据?

时间:2012-04-27 12:56:51

标签: asp.net session exception exception-handling application-error

当应用程序抛出“文件不存在”时,

HttpContext.Current.Session对象为null。 Application_Error事件中的异常。

protected void Application_Error(object sender, EventArgs e)
{
    Exception ex = HttpContext.Current.Server.GetLastError();
    if (ex.Message == "File does not exist." && HttpContext.Current.Session == null)
    {
        if (((System.Web.HttpException)(ex)).GetHttpCode() == 404)
        {
            LogtheException(Session["uname"].ToString());// Throwing the Exception Here
        }
    }
    else
    {
        LogtheException(Session["uname"].ToString());
        Server.Transfer(UrlMaker.ToError(ex.Message.ToString()));
    }
}

抛出异常

Session state is not available in this context.

如果任何css / image文件路径不正确,为什么HttpContext.Current.Session对象为null。相反,它应该抛出FileNotFoundException并且还可以访问会话数据。

1 个答案:

答案 0 :(得分:1)

有人提出了类似的问题here Css和图像请求通常不需要访问会话,因此asp不会将会话加载到内存中,并且您在错误时无法访问它。