asp.net - Global.asax PostAuthenticateRequest 事件绑定(bind)是如何发生的?

标签 asp.net .net events global-asax autoeventwireup

如何使用 PostAuthenticateRequest Global.asax 事件?我关注 this tutorial它提到我必须使用 PostAuthenticateRequest 事件。当我添加 Global.asax 事件时,它创建了两个文件,标记和代码隐藏文件。这是代码隐藏文件的内容

using System;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace authentication
{
    public class Global : System.Web.HttpApplication
    {    
        protected void Application_Start(object sender, EventArgs e)
        {    
        }

        protected void Session_Start(object sender, EventArgs e)
        {    
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {    
        }

        protected void Application_Error(object sender, EventArgs e)
        {    
        }

        protected void Session_End(object sender, EventArgs e)
        {    
        }

        protected void Application_End(object sender, EventArgs e)
        {    
        }
    }
}

现在当我输入
protected void Application_OnPostAuthenticateRequest(object sender, EventArgs e)

它被成功调用。现在我想知道 怎么样了PostAuthenticateRequest 绑定(bind)到这个 Application_OnPostAuthenticateRequest 方法?如何将方法更改为其他方法?

最佳答案

魔术...,一种称为 Auto Event Wireup 的机制,您可以编写相同的原因

Page_Load(object sender, EventArgs e) 
{ 
} 

在您的代码隐藏中,该方法将在页面加载时自动调用。

MSDN description for System.Web.Configuration.PagesSection.AutoEventWireup property :

Gets or sets a value indicating whether events for ASP.NET pages are automatically connected to event-handling functions.



AutoEventWireuptrue , 处理程序会在运行时根据它们的名称和签名自动绑定(bind)到事件。对于每个事件,ASP.NET 搜索根据模式 Page_eventname() 命名的方法。 ,如 Page_Load()Page_Init() . ASP.NET 首先查找具有典型事件处理程序签名的重载(即,它指定 ObjectEventArgs 参数)。如果未找到具有此签名的事件处理程序,ASP.NET 将查找没有参数的重载。更多详情请查看 this answer .

如果您想明确地执行此操作,您将改为编写以下内容
public override void Init()
{
    this.PostAuthenticateRequest +=
        new EventHandler(MyOnPostAuthenticateRequestHandler);
    base.Init();
}

private void MyOnPostAuthenticateRequestHandler(object sender, EventArgs e)
{
}

关于asp.net - Global.asax PostAuthenticateRequest 事件绑定(bind)是如何发生的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4677866/

相关文章:

c# - 为什么SPAN的值没有更新

c# - 系统反射 .Invoke() 无法捕获异步调用方法内的异常

c# - UWP 中的自定义 MarkupExtension

.net - 控件引发的事件应该在 UI 线程上吗?

c# - 在我的 ASP.NET 代码隐藏中使用 StringBuilder 编写 HTML 是一个好习惯吗?

asp.net - jQuery 自动完成组件

c# - Microsoft.WindowsAPICodePack.Shell音频持续时间未获得

.net - 两个 Windows Phone 7 之间的 P2P 连接

c# - 在 CustomControls 中取消订阅事件的模式

android - 触摸行的容器 UI 元素时突出显示 ListView 行