1

当应用程序抛出“文件不存在”时,HttpContext.Current.Session对象为null。 Application_Error事件中发生异常。为什么“文件不存在”。 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并且还可以访问会话数据。

回答

1

类似的问题已经被问here
CSS和图像请求通常不需要访问会话,为此ASP不加载会话到内存中,你没有访问它的错误。

相关问题