locked
Custom Error Page RRS feed

  • Question

  • User1769015664 posted

    How do I display the actual error on my custom error page?

            public void Page_Load(object sender, EventArgs e)
            {
                string sError = Request.QueryString["handler"];
                string sMsg = "An error has occured, please try again!";
                ltMessage.Text = sMsg + "\r\n" + sError;          
            }
    
    <configuration>
      <system.web>
        <customErrors mode="On" defaultRedirect="ErrorPage.aspx?handler=customErrors%20section%20-%20Web.config">
          <error statusCode="404" redirect="ErrorPage.aspx?msg=404&amp;handler=customErrors%20section%20-%20Web.config"/>
        </customErrors>
      </system.web>
    </configuration>

    Global.aspx

    void Application_Error(object sender, EventArgs e)
    {
        Exception exc = Server.GetLastError();
    
        if (exc is HttpUnhandledException)
        {
            // Pass the error on to the error page.
            Server.Transfer("ErrorPage.aspx?handler=Application_Error%20-%20Global.asax", true);
        }
    }
    Friday, October 23, 2020 7:07 AM

All replies

  • User1686398519 posted

    Hi NJ2, 

    1.  CustomError:The custom errors are only issued for those elements handled by ASP.NET.
      1. In other words, if the error occurs outside the application, IIS will reject the error request before it reaches your application code, so that it will not be redirected to a custom error page.
      2. For errors that occur outside the application, you can use httpErrors to implement a custom error page.
    2. I wrote a simple example, you can refer to it.

    Web.config

    <configuration>
        <system.webServer>
    	<httpErrors errorMode="Custom" existingResponse="Replace">
    	    <clear />
                <error statusCode="404" path="/404.aspx" responseMode="ExecuteURL"/>
    	</httpErrors>
        </system.webServer>
    </configuration>

    Global.asax

            void Application_Error(object sender, EventArgs e)
            {
                var exception = HttpContext.Current.Server.GetLastError(); 
                var statusCode = (int)HttpStatusCode.InternalServerError;
                if (exception is HttpException)
                {
                    statusCode = new HttpException(null, exception).GetHttpCode();
                }
                else if (exception is UnauthorizedAccessException)
                {
                    statusCode = (int)HttpStatusCode.Forbidden;
                }
                //Pass the error on to the error page.
                Server.Transfer("ErrorPage.aspx?error="+ statusCode, true);           
            }

    ErrorPage

            protected void Page_Load(object sender, EventArgs e)
            {
                string sError = Request.QueryString["error"];
                string sMsg = "An error has occured, please try again!";
                ltMessage1.Text = sMsg + "\r\n" + sError;
            }

    404.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="404.aspx.cs" Inherits="DailyWebFormsDemo._404" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        404 error!
    </body>
    </html>

    Here is the result.

    Best Regards,

    YihuiSun

    Monday, October 26, 2020 7:48 AM
  • User1769015664 posted

    Hi,

    With your code above, if database connection is not available then the page spins forever.

    Where do I add code for the following exceptions?

    1. SQL Server connection/data error
    2. Path/file error
    Tuesday, October 27, 2020 4:46 AM
  • User1769015664 posted

    The folowing code in Global.ascx.c keeps looping and never gets out of the moving half wheel.

    protected void Page_Load(object sender, EventArgs e)
            {
               var exception = HttpContext.Current.Server.GetLastError();

                var statusCode = (int)HttpStatusCode.InternalServerError;
                if (exception is HttpException)
                {
                    statusCode = new HttpException(null, exception).GetHttpCode();
                }
                else if (exception is UnauthorizedAccessException)
                {
                    statusCode = (int)HttpStatusCode.Forbidden;
                }
                Server.Transfer("ErrorPage.aspx?error=" + statusCode, true);
            }

    Tuesday, October 27, 2020 5:27 AM
  • User1686398519 posted

    Hi NJ2, 

    1. SQL Server connection
      1. When your database connection is wrong, Application_Error will be processed.
      2. You can check out my animated picture below.
    2. Path/file error
      1. Application_Error handler Application and <customErrors> are both application-level error handling.
      2. In other words, they are used to specify the structure of a custom error page to process requests through ASP.NET, and do not process static files.
      3. Some errors are handled by IIS before they reach the application, so you need to use httpErrors to handle errors like static files not found.
      4. The code for the usage of httpErrors has been given in the previous reply, you can refer to it.
    3. protected void Page_Load(object sender, EventArgs e)
      • I am not sure why you wrote this method in the Global.ascx file. If you need to customize the error page, you can modify it according to my code above.

    Here is the result.

    Best Regards,

    YihuiSun

    Tuesday, October 27, 2020 9:04 AM
  • User1769015664 posted

    It still doesn't resolve my issue described in my original post needing to display 'actual' error message...

    1.  I get 404 error if 
        a) Requested page is not found
        b) A file (document) is not found on the server
    I need to be able to notify the user with the correct message, a or b

        c) I get 505 error when there is no connection to the server
        d) I get 505 error when database and/or table is not found
        e) I get 505 error when database and/or table is has permission issues
    I need to be able to display the correct message to the user.

    The detailed information you have provided is a wealth of information to me.  thank you!

    Wednesday, October 28, 2020 2:10 AM
  • User409696431 posted

    "I need to be able to display the correct message to the user."

    I am curious.  Why do you need to do this?  The end user can't fix it, and it is generally better not to try and provide details.

    404 means the resource the URL points to can't be found.  That's all.  It doesn't care what the resource is. You can examine the URL yourself and probably figure it out.  It could be an expected resource is not there, or a bad URL.

    500 errors mean the error is on the server's side.  505 specifically means the server doesn’t support the HTTP version used to make the request.  I don't know why database connection or permission errors would throw that, but it certainly doesn't provide any details about them..

    Give generic error pages to your users and use internal error handling and monitoring, and IIS error logs, to get the details.

    Wednesday, October 28, 2020 5:22 AM