Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Corey Sherman 55 posts 127 karma points
    Nov 12, 2013 @ 15:47
    Corey Sherman
    0

    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?

    Thanks in advance!

  • Paulius Putna 76 posts 134 karma points
    Feb 11, 2016 @ 13:04
    Paulius Putna
    0

    Hi Corey,

    Did you find more about this?

    I am trying to get @Server.GetLastError() however it's null on any template page.

    Thanks,

    Paulius

  • Corey Sherman 55 posts 127 karma points
    Feb 11, 2016 @ 16:04
    Corey Sherman
    0

    Paulius,

    I never got anywhere with this and ended up abandoning it... Sorry!

    Corey

  • Paulius Putna 76 posts 134 karma points
    Feb 11, 2016 @ 16:52
    Paulius Putna
    0

    Hi Corey,

    There are a couple other ways which I am just looking at the moment:

    http://www.wiliam.com.au/wiliam-blog/error-logging-with-email-notifications-in-umbraco

    or using a extension method for UmbracoApplication

        protected new void Application_Error(object sender, EventArgs e)
        {
            base.Application_Error(sender, e);
    
            var lastError = Server.GetLastError();
    
            if (lastError != null)
            {
                _sendMail(lastError.ToString());
            }
        }
    

    I can keep this updated when done.

Please Sign in or register to post replies

Write your reply to:

Draft