1

I want to design a general error page which displays different message by HTTP Status error. Is it possible?

<customErrors mode="RemoteOnly" redirect="GenericErrorPage.htm">

For example, if

 error statusCode="403"

Then displaying

 Access Denied!

Thanks.

1 Answers1

1

Yes, it;s possible, try something like:

  <customErrors mode="On">
    <error redirect="~/GenericError.aspx" statusCode="404"/>
  </customErrors>

However keep in mind that if you are hosting your web application in IIS 7, then you would need to define your custom errors as follows:

  <system.webServer>
    <httpErrors existingResponse="Replace" errorMode="Custom">
      <remove statusCode="404"/>
      <error statusCode="404" path="GenericError.aspx" responseMode="Redirect"  />
    </httpErrors>
  </system.webServer>

Edit 1

If you want to have a generic ASPX error page and display error messages depending on the HTML error status code, you could do the following:

(I just tested and it works)

Add the attribute redirectMode="ResponseRewrite" to your customErrors section:

<customErrors mode="On" defaultRedirect="~/GenericError.aspx" redirectMode="ResponseRewrite" />

In your generic error page (Page_Load event):

    var ex = HttpContext.Current.Server.GetLastError();
    this.lblMessage.Text += "<br/>" + ex.Message + ex.GetType().ToString();

    if (ex is HttpException)
    {
        var nex = ex as HttpException;
        this.lblMessage.Text += " " + nex.GetHttpCode().ToString();

        switch (nex.GetHttpCode())
        {
            case 404:
                // do somehting cool
                break;
            case 503:
                // do somehting even cooler
                break;
            default:
                break;
        }
    }
Jupaol
  • 21,107
  • 8
  • 68
  • 100
  • I mean in GenericError.aspx how to custom the different message? Of course we can have many pages. Each page corresponds a message. But if I just want one page. –  Jul 20 '12 at 20:45
  • Thanks, should I consider IIS 7? Our server is 2008, maybe IIS 7 or above. –  Jul 20 '12 at 23:12