6

I'm handling exceptions with an HttpModule in a manner such as this:

int errorCode = 500;
HttpApplication httpApp = (HttpApplication)sender;

try
{
    if (httpApp.Server != null)
    {
        Exception ex;

        for (ex = httpApp.Server.GetLastError(); ex != null; ex = ex.InnerException)
        {
            try
            {
                HttpException httpEx = ex as HttpException;
                if (httpEx != null)
                    errorCode = httpEx.GetHttpCode();

                // ... retrieve appropriate content based on errorCode
            }
            catch { }
    }
}

For HTTP status codes (ex: 302, 404, 503, etc) everything works great. However, for IIS status codes (ex: 401.5, 403.4, etc), can GetHttpCode retrieve these as its return value is an integer?

user247702
  • 23,641
  • 15
  • 110
  • 157
Bullines
  • 5,626
  • 6
  • 53
  • 93
  • +1. I really like that **for loop** idea using the **InnerException**. Did you ever find a bit of code to pull these IIS error codes? –  Mar 28 '14 at 14:00
  • FYI: Your code neglected to show any call to `Server.ClearError();` If that isn't in there, you should add it. –  Mar 28 '14 at 14:03

2 Answers2

2

You may not be able to. See the second-to-last response here: http://www.velocityreviews.com/forums/t73739-sending-status-as-4011.html. The HTTP RFC doesn't define sub-codes (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html). It looks like it may be an MS only thing - see last response in the first link, which then points to here: http://msdn.microsoft.com/en-us/library/system.web.httpresponse.substatuscode.aspx. While that is how to SET the sub- statuscode, not retrieve it, the interesting thing to me is that it is only supported "with the integrated pipeline mode in IIS 7.0 and at least the .NET Framework version 3.0."

The only other thing I can think of is to look into the HRESULT in the ErrorCode property on the HttpException and see if there's something going on at the bit level where you can figure out the code and sub-code from that.

Don't know if that helps or not.

Dullroar
  • 152
  • 12
  • Further, from that MSDN link: "When you set the SubStatusCode property, ... the code is never sent as part of the final response to the request." – Jon Schneider Sep 14 '15 at 19:06
-2

You don't want the inner exception. You want:

HttpException httpEx = httpApp.Server.GetLastError() as HttpException;
if (httpEx != null)
    errorcode = httpEx == null ? 0 : httpex.GetHttpCode();
Keltex
  • 26,220
  • 11
  • 79
  • 111
  • How will this help me retrieve an IIS status code like 401.5 or 403.4? GetHttpCode still returns an integer. – Bullines Apr 24 '09 at 19:06