Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top