Sorry in advance if this sounds pretty confusing. Glad to make it clear.
The following has been added to web.config : it still works. I donโt know why, but if I do not explicitly indicate 403 errors to redirect to the 403.aspx user page, instead of the GenericError.aspx page, I get a 500 error. However, if I redirect 404 errors to my 404.aspx user page, the 404.aspx code GenericError.aspx written in place and not as expected, and it seems like you never redirected to the actual 404.aspx page (see the commented part of web.config ) Bizarre
CODE:
Web.config file:
<system.webServer> <httpErrors existingResponse="Replace" errorMode="Custom"> <remove statusCode="403"/> <remove statusCode="404"/> <error statusCode="403" path="/Errors/403.aspx" responseMode="Redirect" /> <error statusCode="404" path="/Errors/404.aspx" responseMode="Redirect" /> </httpErrors> </system.webServer>
GenericError.aspx.cs
protected void Page_Load(object sender, EventArgs e) { var ex = HttpContext.Current.Server.GetLastError(); if (ex is HttpException) { var nex = ex as HttpException; //Label in the Main code displays the Error Code from the Error String this.customErrorMessageCode.Text += "Error Code" + " " + nex.GetHttpCode().ToString(); //Label in the Main code displays the Error Message from the Error String this.customErrorMessageLabel.Text += ex.Message.ToString(); //DIV ID in the Main code displays the entire error message this.customErrorMessage.Visible = true; switch (nex.GetHttpCode()) { case 404: this.customErrorMessageCode.Text += " Page Not Found"; this.customErrorMessageImage.Visible = true; // do somehting cool break; case 403: this.customErrorMessageCode.Text += " Forbidden Access"; this.customErrorMessageImage.Visible = true; // do somehting cool break; case 500: this.customErrorMessageCode.Text += " Internal Error"; this.customErrorMessageImage.Visible = true; // do somehting cool break; default: break; } } else { this.customErrorMessageLabel.Text += ex.Message + ex.GetType().ToString(); } }
A source:
CustomError in web.config
source share