I ran into an odd problem in an inherited project where a combination of events leads to unexpected behavior (unexpectedly for me anyway). I was able to duplicate the problem locally; and debug to narrow down the problem. I described in detail below. Thank you Note. This is the play code for the test in its entirety.
Configure Dev Env
- IIS 7.0 on Windows 7
- Integrated AppPool.NET 4.5
- Register HttpModule in web.config
HttpModule
public class Logger : IHttpModule { void IHttpModule.Dispose() {} public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(Context_BeginRequest); } void Context_BeginRequest(object sender, EventArgs e) { var _application = (HttpApplication)sender;
EventTest.aspx
<%@ Page Language="C#" AutoEventWireup="true" Inherits="EventTest.TestPage" Codebehind="EventTest.aspx.cs" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Test</title> </head> <body> <form id="form1" runat="server"> <asp:Label ID="Label1" runat="server"></asp:Label><br /> <asp:Button id="Button1" runat="server" Text="Submit" OnClick="Button1_Click"></asp:Button> </form> </body> </html>
EventTest.aspx.cs
using System; namespace EventTest { public partial class TestPage : System.Web.UI.Page { protected void Page_Load(object sender, System.EventArgs e) { this.Label1.Text += String.Format("Page_Load fired with PostBack {0}<br />", this.IsPostBack.ToString() ); } override protected void OnInit(EventArgs e) {
What's happening
If AutoEventWireUp is True and the HttpModule accesses the Request.Form collection, then no events will fire.
Comment out the Request.Form line in the HttpModule or switch the collection to Request.Headers, etc., then all events fire.
If AutoEventWireUp is False (and Page_Load is registered manually), then all events fire regardless of Request.Form access.
This is not a problem that I need to solve for the project, since I do not use AutoEventWireUp, but I do not understand why this is happening. If someone can shed light on this; I am thankful.
EDIT: if this helps, this is not a problem when accessing from Context_PostAcquireRequestState. I am curious to think because Form is a collection of RO, but there seem to be some changes.
source share