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

发布于 2024-10-11 15:22:45 字数 1456 浏览 2 评论 0原文

如何使用 Global.asax 的 PostAuthenticateRequest 事件?我正在关注本教程并且它提到我必须使用 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如何绑定到这个Application_OnPostAuthenticateRequest方法?我怎样才能将方法更改为其他方法?

How can I use the PostAuthenticateRequest event of Global.asax? I'm following this tutorial and it mentions that I have to use the PostAuthenticateRequest event. When I added the Global.asax event it created two files, the markup and the code-behind file. Here is the content of the code-behind file

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)
        {    
        }
    }
}

Now when I type the

protected void Application_OnPostAuthenticateRequest(object sender, EventArgs e)

It is successfully called. Now I want to know how is the PostAuthenticateRequest bound to this Application_OnPostAuthenticateRequest method? How can I change the method to some other?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁杀 2024-10-18 15:22:45

编写的原因相同

Page_Load(object sender, EventArgs e) 
{ 
} 

Magic...,一种称为“自动事件接线”的机制,与您可以在代码隐藏中

,并且该方法将在页面加载时自动调用。 System.Web 的 MSDN 说明。 Configuration.PagesSection.AutoEventWireup 属性

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

当 AutoEventWireup 为 true 时,处理程序会在运行时根据事件的名称和签名自动绑定到事件。对于每个事件,ASP.NET 都会搜索根据 Page_eventname() 模式命名的方法,例如 Page_Load()Page_Init(). ASP.NET 首先查找具有典型事件处理程序签名的重载(即,它指定 ObjectEventArgs 参数)。如果未找到具有此签名的事件处理程序,ASP.NET 将查找没有参数的重载。更多详细信息请参阅此答案

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

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

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

Magic..., a mechanism called Auto Event Wireup, the same reason you can write

Page_Load(object sender, EventArgs e) 
{ 
} 

in your code-behind and the method will automatically be called when the page loads.

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.

When AutoEventWireup is true, handlers are automatically bound to events at run time based on their name and signature. For each event, ASP.NET searches for a method that is named according to the pattern Page_eventname(), such as Page_Load() or Page_Init(). ASP.NET first looks for an overload that has the typical event-handler signature (that is, it specifies Object and EventArgs parameters). If an event handler with this signature is not found, ASP.NET looks for an overload that has no parameters. More details in this answer.

If you wanted to do it explicitly you would write the following instead

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

private void MyOnPostAuthenticateRequestHandler(object sender, EventArgs e)
{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文