Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom error handling Asp.Net

On my web application, I had configured my 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>

For explaining propouses I only captured the 403 and 404 error (and the defaultRedirect obviously). But I would like to get more details of the error on the page: Error.aspx somehow; but not creating each page for each kind of error. Is there a way to include certain code on my error page (Error.aspx) to get the detail of what raised that error?.

PD. I'm using C#.

like image 491
lidermin Avatar asked Apr 08 '26 07:04

lidermin


1 Answers

Just to add to the conversation and apply what others already suggested, this is how you can use what Mun suggested showing the error in 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);

        }

In the Error.aspx page put this code inside the Body tag:

<p>
    <i><%= Context.Error.InnerException.Message.ToString() %></i>
</p>
like image 148
Ricardo Sanchez Avatar answered Apr 10 '26 20:04

Ricardo Sanchez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!