gpt4 book ai didi

c# - 从 HttpModule 访问 Request.Form 中断 AutoEventWireUp

转载 作者:行者123 更新时间:2023-11-30 13:01:35 25 4
gpt4 key购买 nike

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

开发环境设置

  • 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 集合,但似乎有一些修改。

最佳答案

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

关于c# - 从 HttpModule 访问 Request.Form 中断 AutoEventWireUp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18574959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com