开发者

System.Web.HttpException was unhandled by user code

I have been using

protected void Application_Error(Object sender, EventArgs e)
{
    HttpException ex = Server.GetLastError() as HttpException;
    Exception ex_T = ex;
    S开发者_JAVA技巧ession["Ex"] = ex;
    Session["Ex_T"] = ex_T;
    Response.Redirect("~/errorPage.aspx" );
}

function for exception handling.

Here when i login to the application " sometime"

an exception occurs which details as

System.Web.HttpException was unhandled by user code
  Message="Session state is not available in this context."
  Source="System.Web"
  ErrorCode=-2147467259
  StackTrace:
       at System.Web.HttpApplication.get_Session()
       at Tools.Global.Application_Error(Object sender, EventArgs e) in C:\Myproject\Tools\Global.asax.cs:line 67
       at System.EventHandler.Invoke(Object sender, EventArgs e)
       at System.Web.HttpApplication.RaiseOnError()
  InnerException: 

i cannot track the reason

Can any one help me out

Thanks in advance


I can see 2 problems here. The first is that the error occurred outside a session or before a session is created. As you can see from the stack trace, the session was not initialized or has been exited. Sessions cannot be started and should not be accessed within Application_Error.

The second problem is that GetLastError may return an Exception that is not a HttpException. In that case, ex gets assigned null. ex_t is also assigned null from ex.

Exception ex = Server.GetLastError();
if (ex != null)
{
    HttpException hex = ex;
    if (hex != null)
        Session["Ex"] = hex; // try to do something else instead of accessing session
}
Session["Ex_T"] = ex; // try to do something else instead of accessing session
Response.Redirect("~/errorPage.aspx" );


First check if the Session is not null before accessing the values.

Secondly GetLastError may return an Exception that is not a HttpException. First try see the if the (ex is HttpException) then assign it to another HttpException variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜