为什么Application_Error "File does not exist."例外无法访问会话数据?



当应用程序在事件中引发"文件不存在"异常时,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.

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

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

最新更新