2015-07-20 83 views
5

应用程序事件如果我要处理应用程序事件在我的ASP.NET应用程序,我会在我的Global.asax注册处理程序:注册处理程序在ASP.NET 5

protected void Application_BeginRequest(object sender, EventArgs e) 
{ ... } 

Global.asax已经从ASP删除。 NET 5.我现在如何处理这些事件?

+0

从我所能找到的,我相信这一切都在ASP.NET 5的'startup.cs'文件中完成。http://wildermuth.com/2015/3/2/A_Look_at_ASP_NET_5_Part_2_-_Startup –

+0

@德鲁肯尼迪 - 嘿嘿你更快,甚至提供相同的链接 – 2015-07-20 13:09:32

+0

@ Tanis83是!我会提供它作为答案,但实际上这只是一个猜测。 :p –

回答

-1

ASP.NET应用程序可以在没有global.asax的情况下运行。

HTTPModule是global.asax的替代方案。

阅读更多here

1

在ASP.NET 5中为每个请求运行一些逻辑的方法是通过中间件。下面是一个简单的中间件:

public class FooMiddleware 
{ 
    private readonly RequestDelegate _next; 

    public FooMiddleware(RequestDelegate next) 
    { 
     _next = next; 
    } 

    public async Task Invoke(HttpContext context) 
    { 
     // this will run per each request 
     // do your stuff and call next middleware inside the chain. 

     return _next.Invoke(context); 
    } 
} 

然后,您可以在您的Startup类注册此:

public class Startup 
{ 
    public void Configure(IApplicationBuilder app) 
    { 
     app.UseMiddleware<FooMiddleware>(); 
    } 
} 

在这里看到more information on Middlewares in ASP.NET 5

对于任何应用程序开始级调用,请参阅application startup documentation

+0

我是否也将中间件用于所有其他事件? 'Application_Start','Application_AuthenteicateRequest','Session_Start'等等......? –

+0

中间件的调用方法将根据每个请求调用,具体取决于中间件位于链中的位置。对于应用程序启动,在'Startup'方法中有几个地方可以像'Startup''','ConfigureServices'方法和'Configure'方法一样标签。 – tugberk

+0

在这里查看关于'Startup'类的更多信息:http://docs.asp.net/en/latest/conceptual-overview/understanding-aspnet5-apps.html#application-startup – tugberk

相关问题