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

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

问题描述

如何使用Global.asax的 PostAuthenticateRequest 事件?我正在遵循本教程和它提到我必须使用 PostAuthenticateRequest 事件。当我添加了Global.asax事件时,它创建了两个文件,标记和代码隐藏文件。以下是使用System的代码隐藏文件的内容

  
使用System.Web;
使用System.Web.Security;
使用System.Web.SessionState;

命名空间验证
{
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 方法?如何将方法更改为其他?

解决方案

魔术...,一种称为自动事件绑定 / em>,同样的原因你可以写

  Page_Load(object sender,EventArgs e)
{
}

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

MSDN对的描述System.Web.Configuration.PagesSection.AutoEventWireup 属性


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


AutoEventWireup true 时,处理程序根据其名称和签名在运行时自动绑定到事件。对于每个事件,ASP.NET搜索根据模式 Page_eventname()命名的方法,例如 Page_Load() Page_Init()。 ASP.NET首先查找具有典型事件处理程序签名的重载(也就是说,它指定 Object EventArgs 参数)。如果没有找到具有该签名的事件处理程序,ASP.NET将查找一个没有参数的重载。 此答案的详细信息。



如果你想做显式地,你会写下面的代码

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

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


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?

解决方案

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

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

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