AutoEventWireUp terminates access to Request.Form from HttpModule

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; // Comment line below (or change to any other collection) while AutoEventWireUp is true - no problems foreach (string key in _application.Request.Form.AllKeys) { } } } 

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) { // Uncomment this and set AutoEventWireUp to False - no problems //this.Load += new EventHandler(Page_Load); base.OnInit(e); } protected void Button1_Click(object sender, System.EventArgs e) { this.Label1.Text += "Button1_Click Fired<br />"; } } } 

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.

+4
source share
1 answer

Try adding this function to your HttpModule:

 public void Init(HttpApplication objApplication) { objApplication.PreRequestHandlerExecute += new EventHandler(this.Application_PreRequestHandlerExecute); } private void Application_PreRequestHandlerExecute(object objSender, EventArgs objE) { Page objPage = (Page)(objSender as HttpApplication).Context.Handler; objPage.AutoEventWireup = true; } 
0
source

Source: https://habr.com/ru/post/1500155/


All Articles