Individual error handling Asp.Net

In my web application, I configured the web.config file to set customerrors to ON, so here it is:

<customErrors mode="On" defaultRedirect="Error.aspx"> <error statusCode="403" redirect="Error.aspx" /> <error statusCode="404" redirect="Error.aspx" /> </customErrors> 

To explain the motives, I only fixed error 403 and 404 (and, obviously, the default value is Redirect). But I would like to receive more detailed information about the error on the page: Error.aspx somehow; but not creating every page for every type of error. Is there a way to include specific code in my error page (Error.aspx) to get detailed information about what caused this error?

PD. I am using C #.

+3
source share
4 answers

Just add to the conversation and apply what others have already suggested, here is how you can use what Moon suggested to show the error on the Error.aspx page:

 protected void Application_Error(object sender, EventArgs e) { //Get last error Exception ex = Server.GetLastError(); ex = ex.GetBaseException(); //display error to user Context.AddError(ex); Server.Transfer("Error.aspx", true); } 

On the Error.aspx page, enter this code inside the Body tag:

 <p> <i><%= Context.Error.InnerException.Message.ToString() %></i> </p> 
+2
source

btw, have you tried using ELMAH . This is the easiest way to ensure excellent error reporting and reporting.

EDIT: - I know this is not related to Error.aspx. I just suggested a good way to register and report exceptions.

0
source

In most situations, you can use Server.GetLastError () to get the error that caused the redirect.

There is a chance that you will encounter a race condition if two errors occur almost simultaneously, but one connection is faster than the other.

0
source

You can catch and log errors by handling the Application_Error event in Global.asax.

 protected void Application_OnError(object sender, EventArgs e) { // Get the last error Exception exception = Server.GetLastError(); // Do something with the error here ErrorLogger.Log(exception); } 

You may want to use something like ELMAH or Log4net to log errors .

0
source

Source: https://habr.com/ru/post/1483789/


All Articles