应用程序事件如果我要处理应用程序事件在我的ASP.NET应用程序,我会在我的Global.asax
注册处理程序:注册处理程序在ASP.NET 5
protected void Application_BeginRequest(object sender, EventArgs e)
{ ... }
Global.asax
已经从ASP删除。 NET 5.我现在如何处理这些事件?
应用程序事件如果我要处理应用程序事件在我的ASP.NET应用程序,我会在我的Global.asax
注册处理程序:注册处理程序在ASP.NET 5
protected void Application_BeginRequest(object sender, EventArgs e)
{ ... }
Global.asax
已经从ASP删除。 NET 5.我现在如何处理这些事件?
在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。
我是否也将中间件用于所有其他事件? 'Application_Start','Application_AuthenteicateRequest','Session_Start'等等......? –
中间件的调用方法将根据每个请求调用,具体取决于中间件位于链中的位置。对于应用程序启动,在'Startup'方法中有几个地方可以像'Startup''','ConfigureServices'方法和'Configure'方法一样标签。 – tugberk
在这里查看关于'Startup'类的更多信息:http://docs.asp.net/en/latest/conceptual-overview/understanding-aspnet5-apps.html#application-startup – tugberk
从我所能找到的,我相信这一切都在ASP.NET 5的'startup.cs'文件中完成。http://wildermuth.com/2015/3/2/A_Look_at_ASP_NET_5_Part_2_-_Startup –
@德鲁肯尼迪 - 嘿嘿你更快,甚至提供相同的链接 – 2015-07-20 13:09:32
@ Tanis83是!我会提供它作为答案,但实际上这只是一个猜测。 :p –