如何使用 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.
当
AutoEventWireup
是 true
, 处理程序会在运行时根据它们的名称和签名自动绑定(bind)到事件。对于每个事件,ASP.NET 搜索根据模式 Page_eventname()
命名的方法。 ,如 Page_Load()
或 Page_Init()
. ASP.NET 首先查找具有典型事件处理程序签名的重载(即,它指定 Object
和 EventArgs
参数)。如果未找到具有此签名的事件处理程序,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/