The HttpRequest.Form collection is cleared after a managed HttpModule

I am suffering with this problem, I cannot find an explanation.

You have a website that is handling ASP and ASPX requests. All requests are run through a custom managed module named MyModule, lets say "for logging purposes".

This is WebConfig:

<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="MyModule" type="MySample.MyModule" preCondition="managedHandler" />
    </modules>
  </system.webServer>
</configuration>

      

So, if the form is submitted to / action.asp via AJAX, html form or whatever, to /action.asp, I can see and print data in the Request.Form collection.

This is / action.asp

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
  dim count 
  count = Request.Form.Count
  Response.write("Result: " & count)
  Response.End() 
%>

      

But if in my custom managed module I just "PEEK" the form collection before it is processed by the ASP page, the collection disappears, it is no longer available to /action.asp This is MyModule:

namespace MySample
{

  public class MyModule : IHttpModule
  {
    public MyModule()
    {
    }

    public void Init(HttpApplication context)
    {
      context.BeginRequest += context_BeginRequest;
    }

    void context_BeginRequest(object sender, EventArgs e)
    {
      /*
       * ALL REQUESTS PASS THROUGH THIS POINT BEFORE CONTINUING.
       * COUNTING THE ITEMS ON THE FORM WILL CAUSE THE FORM-COLLECTION 
       * TO BE CLEARED WHEN IT HANDLED BY ASP.
       */
      int count = HttpContext.Current.Request.Form.Count;
    }

    public void Dispose()
    {
    }

  }

}

      

It is very strange. If I "comment out" the counting line, the form collection is rendered unchanged in the ASP page. I just need to look into it to get it running.

I want to find an explanation for this backed up by some documentation, not just guesswork.

I cannot set runAllManagedModulesForAllRequests false, this is not an option.

I have debugged the request through various .NET method calls and many things happen when you request a Form object of a .NET HttpRequest object.

        // Form collection
        ///    Gets a collection of Form variables.
        public NameValueCollection Form {
            get {
                EnsureForm();

                if (_flags[needToValidateForm]) {
                    _flags.Clear(needToValidateForm);
                    ValidateHttpValueCollection(_form, RequestValidationSource.Form);
                }

                return _form;
            }
        }

        // Populates the Form property but does not hook up validation.
        internal HttpValueCollection EnsureForm() {
            if (_form == null) {
                _form = new HttpValueCollection();

/// THE FOLLWING METHOD AS A LOT OF SUB-CALLS AS WELL
                if (_wr != null)
                    FillInFormCollection();

                _form.MakeReadOnly();
            }

            return _form;
        }

      

Am I the expected behavior? What is the documentation or reasoning to support this behavior?

+3


source to share





All Articles