CustomError in web.config

I want to create a generic error page displaying another HTTP Status error message. Is it possible?

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

For example, if

  error statusCode="403" 

Then display

  Access Denied! 

Thanks.

+1
source share
1 answer

Yes, maybe try something like:

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

However, keep in mind that if you host your web application in IIS 7, you will need to identify 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> 

Change 1

If you want to create a common ASPX error page and display error messages depending on the HTML error status code, you can do the following:

(I just tested and works)

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

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

On the common 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; } } 
+1
source

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


All Articles