1

当应用程序抛出“文件不存在”时,HttpContext.Current.Session 对象为空。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/图像文件路径不正确,为什么 HttpContext.Current.Session 对象为空。相反,它应该抛出 FileNotFoundException 并且还可以访问会话数据。

4

1 回答 1

1

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

于 2012-04-27T13:04:47.133 回答