Global.asax PostAuthenticateRequest 事件绑定是如何发生的?

2024-05-15

我怎样才能使用发布验证请求Global.asax 事件?我正在跟进本教程 http://www.asp.net/security/tutorials/forms-authentication-configuration-and-advanced-topics-cs它提到我必须使用发布验证请求事件。当我添加 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)

就调用成功了。现在我想知道怎么样发布验证请求与此绑定Application_PostAuthenticateRequest方法?我怎样才能将方法更改为其他方法?


魔法……,一种叫做自动事件连线,同样的原因你可以写

Page_Load(object sender, EventArgs e) 
{ 
} 

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

MSDN 描述System.Web.Configuration.PagesSection.AutoEventWireup财产 http://msdn.microsoft.com/en-us/library/system.web.configuration.pagessection.autoeventwireup.aspx:

获取或设置一个值,该值指示 ASP.NET 页的事件是否自动连接到事件处理函数。

When AutoEventWireup is true,处理程序在运行时根据事件的名称和签名自动绑定到事件。对于每个事件,ASP.NET 都会搜索根据模式命名的方法Page_eventname(), 例如Page_Load() or Page_Init()。 ASP.NET 首先查找具有典型事件处理程序签名的重载(即,它指定Object and EventArgs参数)。如果未找到具有此签名的事件处理程序,ASP.NET 将查找没有参数的重载。更多详情请参阅这个答案 https://stackoverflow.com/a/2059139/57428.

如果您想明确地执行此操作,则可以编写以下内容

public override void Init()
{
    this.PostAuthenticateRequest +=
        new EventHandler(MyOnPostAuthenticateRequestHandler);
    base.Init();
}

private void MyOnPostAuthenticateRequestHandler(object sender, EventArgs e)
{
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Global.asax PostAuthenticateRequest 事件绑定是如何发生的? 的相关文章

随机推荐