How to retrieve Http Error Code in Application_Error in file Global.asax.cs
-
Hello, I would like to receive an e-mail when an Error Occurs on my WebSite but not for errors like "401 402 404...". So before to send it I test errors. This is my code:
public class Global : System.Web.HttpApplication { protected void Application_Error(Object sender, EventArgs e) { System.Web.HttpException MyHttpError; MyHttpError = System.Web.HttpException.CreateFromLastError(Server.GetLastError().Message) ; if(!(MyHttpError.GetHttpCode()==404)) { //Send an email and redirect } else // just redirect } }
The problem is that in my Spy Window the value of Server.GetLastError()._httpCode is 404 but the value of httpErreur.GetHttpCode() is 500 !! Please explain me or give me another solution. -- modified at 10:02 Friday 20th January, 2006 -
Hello, I would like to receive an e-mail when an Error Occurs on my WebSite but not for errors like "401 402 404...". So before to send it I test errors. This is my code:
public class Global : System.Web.HttpApplication { protected void Application_Error(Object sender, EventArgs e) { System.Web.HttpException MyHttpError; MyHttpError = System.Web.HttpException.CreateFromLastError(Server.GetLastError().Message) ; if(!(MyHttpError.GetHttpCode()==404)) { //Send an email and redirect } else // just redirect } }
The problem is that in my Spy Window the value of Server.GetLastError()._httpCode is 404 but the value of httpErreur.GetHttpCode() is 500 !! Please explain me or give me another solution. -- modified at 10:02 Friday 20th January, 2006Hi there, That's because the method
CreateFromLastError
returns a new HttpException, and it does not assign the httpcode of the last error to the new HttpException. Instead, it simply sets theHResult
value from the last error by calling theHResultFromLastError
method. So when theGetHttpCode
method of this HttpException is called, it simply returns the default error code500
. If you want to create an HttpException wrapping the last error, you simply create an HttpException object with the new keyword and pass the last exception in as the internal exception of the new HttpException. -
Hi there, That's because the method
CreateFromLastError
returns a new HttpException, and it does not assign the httpcode of the last error to the new HttpException. Instead, it simply sets theHResult
value from the last error by calling theHResultFromLastError
method. So when theGetHttpCode
method of this HttpException is called, it simply returns the default error code500
. If you want to create an HttpException wrapping the last error, you simply create an HttpException object with the new keyword and pass the last exception in as the internal exception of the new HttpException.Ok, thanks I simply do :
System.Web.HttpException httpErreur =(HttpException)Server.GetLastError(); if(!(httpErreur.GetHttpCode()==404)) { } else { }
and it works