如果我想在我的 ASP.NET 应用程序中处理应用程序事件,我会在我的 Global.asax
中注册一个处理程序:
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 .
关于c# - 在 ASP.NET 5 中注册应用程序事件的处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31517135/