Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

c# - How can I get the IIS substatus code from an Exception?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...