HttpRequest.Form collection gets cleared after Managed HttpModule

121 Views Asked by At

I’m suffering this issue I can’t find an explanation for.

Have a website that handles ASP and ASPX requests. All requests run through a custom managed module, named MyModule, let’s say for “logging purposes”.

This is the 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 a form is posted to /action.asp via AJAX, an html form, or whatever, on /action.asp I can see and print the data on 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” at the form collection before it’s handled by the ASP page, the collection disappears, it’s no longer available to /action.asp This is the 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'S HANDLED BY ASP.
       */
      int count = HttpContext.Current.Request.Form.Count;
    }

    public void Dispose()
    {
    }

  }

}

It’s extremely odd. If I “comment” the count line, the form collection is handled unmodified to the ASP page. I just have to peek at it to run it.

I want to find an explanation for this backed up with some documentation, not just a hunch.

I can’t set to false the runAllManagedModulesForAllRequests, this is not an option.

I debugged the request through different .NET method calls, and many things happen when you query the Form object on .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;
        }

Is what I am experiencing an expected behavior ? What’s the documentation or the reasoning to back up this behavior ?

0

There are 0 best solutions below