« Previous Next »

Answered Thread: HTTP Status Codes & IIS Versions

Last post 10-20-2009 3:11 AM by Leo Tang - MSFT. 1 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (2 items)

Sort Posts:

  • 10-16-2009, 10:07 AM

    • codeboy
    • Not Ranked
    • Joined on 06-10-2002, 8:58 PM
    • Wilton, NY
    • Posts 10

    HTTP Status Codes & IIS Versions

     I'm having trouble using the HTTP Status codes from a managed http module in asp.net on different versions of IIS. 

    IIS 5.1 

     200 127.0.0.1 http://localhost/httpModuleTest/default.aspx 127.0.0.1 10/16/2009 9:49:27 AM
    200 127.0.0.1 http://localhost/httpModuleTest/notfound.aspx 127.0.0.1 10/16/2009 9:49:32 AM

    IIS6

    200 127.0.0.1 http://iis6.example.com/Default.aspx 127.0.0.1 10/16/2009 9:51:57 AM
    200 127.0.0.1 http://iis6.example.com/missing.aspx 127.0.0.1 10/16/2009 9:52:03 AM

    IIS7

    200 127.0.0.1 http://iis7.example.com/default.aspx 127.0.0.1 10/16/2009 10:02:58 AM
    404 127.0.0.1 http://iis7.example.com/missing.aspx 127.0.0.1 10/16/2009 10:03:04 AM
     

    Basically for anything i've tried below IIS7 returns 200 OK no matter what.   IIS7 reacts correctly or rather asp.net reacts correctly on IIS7 and returns the expected status code.  Is there any way of getting the correct status code from IIS when not running 7.0?

    Sample code:

        public class SampleHttpModule : IHttpModule
        {
            #region IHttpModule Members

            public void Dispose()
            {

            }

            public void Init(HttpApplication context)
            {
             
                context.EndRequest += new EventHandler(context_EndRequest);
            }
            
            void context_EndRequest(object source, EventArgs e)
            {
                HttpApplication application = (HttpApplication)source;
                HttpContext context = application.Context;

                string logFile = application.Server.MapPath("~/log.txt");
               
                StreamWriter writer = File.AppendText(logFile);
                writer.WriteLine(String.Format("{0} {1} {2} {3} {4}", context.Response.StatusCode, context.Request.UserHostName, context.Request.Url, context.Request.UserHostAddress, DateTime.Now));
                writer.Close();
            }

            #endregion
        }

     

    Scott Sargent

    Visit my Blog
  • 10-20-2009, 3:11 AM In reply to

    Answered Re: HTTP Status Codes & IIS Versions

    Hi,

    You can use the PreSendRequestHeaders or PreSendRequestContent Event. Then the status code should be logged correctly on both IIS7.0 and earlier versions.

    Leo Tang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (2 items)
Microsoft Communities