【问题标题】:Accessing Request.Form from HttpModule Breaks AutoEventWireUp从 HttpModule 访问 Request.Form 会中断 AutoEventWireUp
【发布时间】:2013-09-05 15:58:19
【问题描述】:

我在一个遗留项目中遇到了一个奇怪的问题,其中事件的组合导致了意外的行为(无论如何我都没有预料到)。我能够在本地复制该问题;并调试以缩小问题范围。我已经在下面详细说明了。谢谢。 注意:这是完整的测试重现代码。

开发环境设置

  • Windows 7 上的 IIS 7.0
  • 集成的 AppPool .NET 4.5
  • 在 web.config 中注册 HttpModule

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 />";
        }
    }
}

会发生什么

  • 如果 AutoEventWireUp 为 True,并且 HttpModule 访问 Request.Form 集合,则不会触发任何事件。

  • 在 HttpModule 中注释 Request.Form 行或将集合切换为 Request.Headers 等,然后所有事件都会触发。

  • 如果 AutoEventWireUp 为 False(并且 Page_Load 是手动注册的),则无论 Request.Form 访问如何,都会触发所有事件。

这不是我需要为项目解决的问题,因为我不使用 AutoEventWireUp,但我不明白为什么会发生这种情况。如果有人可以对此有所了解;我很感激。

编辑:如果有帮助,从 Context_PostAcquireRequestState 访问时这不是问题。我很好奇想法,因为 Form 是一个 RO 集合,但似乎有一些修改。

【问题讨论】:

    标签: c# asp.net httpmodule


    【解决方案1】:

    尝试将此函数添加到您的 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;
    }
    

    【讨论】:

    • 有趣的想法。不幸的是,我无法在我更新的机器上重现该问题。我可能必须将 VM 恢复到 2013 年的版本,然后查看更新是否解决了问题。 2013 年,我们在三个环境中复制了它。
    猜你喜欢
    • 1970-01-01
    • 2010-09-21
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多