HttpContext.Current.Session object is null when application throws "File does not exist." exception in Application_Error event.

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()));
    }
}

Throwing Exception

Session state is not available in this context.

Why HttpContext.Current.Session object is null when if any css/image file path is not correct. Instead it should throw FileNotFoundException and also have access to Session Data.

有帮助吗?

解决方案

A similar question has been asked here
Css and image requests normally don't need to access the session, therefor asp does not load the session into memory and you don't have access to it on error.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top