Access to the forms collection in IHttpModule means that the event handler does not receive a call on the default page

OK, this is weird. I created a simple sample site to demonstrate this problem. In it, I have a Default.aspx page on which there is a button:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <p><asp:Button OnClick="ButtonClick" Text="Button" runat="server" />
    </p>
    <asp:Label ID="output" runat="server" />
</asp:Content>

The code only sets the label text when the button is pressed:

    protected void ButtonClick(object sender, EventArgs e)
    {
        output.Text = "Button Click!!";
    }

Then I have an IHttpModule that is called for each request:

public class SampleModule : IHttpModule
{
    public void Dispose()
    {
    }

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

    private void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication application = sender as HttpApplication;
        if(application == null)
        {
            return;
        }
        HttpContext context = application.Context;
        if(context == null)
        {
            return;
        }

        string text = "queryStringParam";

        var value = context.Request[text];
        var boolValue = value == null;
    }
}

Again, this is just a demonstration, but the point here is that I turn to Request to get the value from the query string. If I run this in Cassini, everything will be fine. However, when I run this in IIS, this is what happens. When I launch the site at:

http://mysamplesite.dev/

, . , , . , :

http://mysamplesite.dev/Default.aspx

, , !

:

string text = "queryStringParam";

var value = context.Request.QueryString[text];
var boolValue = value == null;

: QueryString context.Request. , , , Default.aspx URL?!

, Reflector, , HttpRequest:

public string this[string key]
{
    get
    {
        string str = this.QueryString[key];
        if (str != null)
        {
            return str;
        }
        str = this.Form[key];
        if (str != null)
        {
            return str;
        }
        HttpCookie cookie = this.Cookies[key];
        if (cookie != null)
        {
            return cookie.Value;
        }
        str = this.ServerVariables[key];
        if (str != null)
        {
            return str;
        }
        return null;
    }
}

, , , . , . :

string text = "queryStringParam";

var value = context.Request.QueryString[text];
var boolValue = value == null;

var value2 = context.Request.Form[text];
var boolValue2 = value2 == null;

! , , IHttpModule, - PostBack, .

- , ? ASP.Net MVC, ASP.Net , , , .

+3
2
  • ( )
  • fiddler , - - .
+1

"http://mysamplesite.dev/Default.aspx", context_BeginRequest , . , "http://mysamplesite.dev/" , - context_BeginRequest .

"http://mysamplesite.dev/" context_BeginRequest Form, context.Request["queryStringParam"], ( _form context.Request ). Page_Load .

, , ASP.NET "/" , "/Default.aspx", BeginRequest . , ASP.NET , . , , ASP.NET , HttpRequest, , context_BeginRequest (, , ).

. ASP.NET(, Server.Transfer "/" "/Default.aspx" - ), Fiddler2 , "http://mysamplesite.dev/" .

+1

All Articles