如何Global.asax中PostAuthenticateRequest事件绑定发生? [英] How does Global.asax PostAuthenticateRequest event binding happen?

查看:265
本文介绍了如何Global.asax中PostAuthenticateRequest事件绑定发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用 PostAuthenticateRequest 事件Global.asax中的?我关注的<一个href="http://www.asp.net/security/tutorials/forms-authentication-configuration-and-advanced-topics-cs"相对=nofollow>本教程并提到,我必须使用 PostAuthenticateRequest 事件。当我加在Global.asax事件它创造了两个文件,​​标记和code-隐藏文件。这里是code-隐藏文件的内容

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

现在,当我键入

protected void Application_OnPostAuthenticateRequest(object sender, EventArgs e)

这是成功调用。现在我想知道的是如何在 PostAuthenticateRequest 绑定到这个 Application_OnPostAuthenticateRequest 方式?我怎么可以在方法切换到其他的?

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?

推荐答案

魔术......,被称为机构的自动事件Wireup 的,你可以写同样的道理

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

Page_Load(object sender, EventArgs e) 
{ 
} 

在code-后面,该方法将自动被调用时,页面加载。

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

<一个href="http://msdn.microsoft.com/en-us/library/system.web.configuration.pagessection.autoeventwireup.aspx"相对=nofollow> MSDN说明 System.Web.Configuration.PagesSection.AutoEventWireup 属性:

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

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

AutoEventWireup ,处理程序自动绑定到根据他们的名字和签名在运行时的事件。对于每个事件,ASP.NET搜索一个是根据模式命名方法 Page_eventname(),如的Page_Load() Page_Init()。 ASP.NET首先查找具有典型的事件处理程序的签名(即,它指定对象 EventArgs的参数)。如果没有找到一个事件处理程序与此签名,ASP.NET查找具有没有参数的重载。在这个答案。

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

这篇关于如何Global.asax中PostAuthenticateRequest事件绑定发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆