Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing the Form collection in an IHttpModule causes event Handler not to get called on Default page
    primarykey
    data
    text
    <p>Ok, this is a weird one. I've created a simple sample site to demonstrate the issue. In it, I have a Default.aspx Page that has a button on it:</p> <pre><code>&lt;asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"&gt; &lt;p&gt;&lt;asp:Button OnClick="ButtonClick" Text="Button" runat="server" /&gt; &lt;/p&gt; &lt;asp:Label ID="output" runat="server" /&gt; &lt;/asp:Content&gt; </code></pre> <p>The code behind just sets the label text on the button click:</p> <pre><code> protected void ButtonClick(object sender, EventArgs e) { output.Text = "Button Click!!"; } </code></pre> <p>I then have an IHttpModule that gets called for every request:</p> <pre><code>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; } } </code></pre> <p>Again, this is just a demo, but the point here is, I'm accessing the Request, to get the value off the query string. If I run this in Cassini, it all works fine. However when I run this in IIS, this is what happens. When I run the site at:</p> <pre><code>http://mysamplesite.dev/ </code></pre> <p>and then click on the button, nothing happens. The page just reloads, but my event handler for the button never gets called, and subsequently the label text never gets updated. However if I then run it at:</p> <pre><code>http://mysamplesite.dev/Default.aspx </code></pre> <p>and then click the button, it works fine and my event handler does get called! </p> <p>After digging around some I changed the code in the module to this:</p> <pre><code>string text = "queryStringParam"; var value = context.Request.QueryString[text]; var boolValue = value == null; </code></pre> <p>Notice, here I'm accessing the QueryString property directly, and not going to context.Request. When I changed it to this, it works fine regardless of whether or not I had Default.aspx in the url?!</p> <p>Next step I did was, I looked at Reflector to see what the code of the HttpRequest indexer property actually does:</p> <pre><code>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; } } </code></pre> <p>Seems harmless enough, it just checks various collections for me, so I don't need to check each one individually. So then I'm wondering, which one of those calls breaks it. I then changed my module to this:</p> <pre><code>string text = "queryStringParam"; var value = context.Request.QueryString[text]; var boolValue = value == null; var value2 = context.Request.Form[text]; var boolValue2 = value2 == null; </code></pre> <p>And now it's broken again! So to make a long story short, just by <strong>accessing</strong> the Form collection on the request in the IHttpModule, I somehow screw up the PostBack, and the event never gets fired. </p> <p>Does anyone have any idea why this is happening? I'm more of an ASP.Net MVC guy, I don't know ASP.Net and all the shtick it pulls behind the scenes well enough to really have a clue as to why this is happening. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload