How to get HTTP Error Code from the Server in a Custom Error Page
I am trying to create a .NET UserControl for my Custom Error Page that gets the HTTP Error Code from the Server and then uses that to determine with message to display, but unfortunately it looks like Umbraco might be clearning the Error before it redirects to my Custom Error Page.
The goal here is to have one custom error page that is smart enough to get the error code from the server and display the appropriate message without the need to create a page for each specific error message.
To better clarify what I have going on, here's an overview.
I already updated umbracoSettings.config to redirect 404 to my custom page and this part is working.
In my usercontrol, I am trying to get the last error from the server by doing the following:
var Server = HttpContext.Current.Server; Exception httpException = Server.GetLastError();
httpException is always null, meaning that the last error on the Server is likely being cleared before the custom page is loading. This is where I am getting stuck. When I test httpException, it's always null and I display a generic error message, never giving me a chance to get the http code.
It's worth mentioning that I am getting to the custom Error Page and it is displaying properly, I just can't get the Error from the server that sent the user to this page, which in this case is a 404.
Is Umbraco clearing the error? If so, is there an easy way to get it to stop doing that without having to modify anything in Umbraco Core? Am I barking up the wrong tree needing to look for the http error code that sent us to this page elsewhere?
How to get HTTP Error Code from the Server in a Custom Error Page
I am trying to create a .NET UserControl for my Custom Error Page that gets the HTTP Error Code from the Server and then uses that to determine with message to display, but unfortunately it looks like Umbraco might be clearning the Error before it redirects to my Custom Error Page.
The goal here is to have one custom error page that is smart enough to get the error code from the server and display the appropriate message without the need to create a page for each specific error message.
To better clarify what I have going on, here's an overview.
I already updated umbracoSettings.config to redirect 404 to my custom page and this part is working.
In my usercontrol, I am trying to get the last error from the server by doing the following:
httpException is always null, meaning that the last error on the Server is likely being cleared before the custom page is loading. This is where I am getting stuck. When I test httpException, it's always null and I display a generic error message, never giving me a chance to get the http code.
It's worth mentioning that I am getting to the custom Error Page and it is displaying properly, I just can't get the Error from the server that sent the user to this page, which in this case is a 404.
Is Umbraco clearing the error? If so, is there an easy way to get it to stop doing that without having to modify anything in Umbraco Core? Am I barking up the wrong tree needing to look for the http error code that sent us to this page elsewhere?
Thanks in advance!
Paulius,
I never got anywhere with this and ended up abandoning it... Sorry!
Corey
The custom error page does not know about the error. Here is a decent set of articles on .net error handling
https://docs.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/deploying-web-site-projects/displaying-a-custom-error-page-cs
is working on a reply...