在 ASP.NET 5 中注册应用程序事件的处理程序

Register handlers for application events in ASP.NET 5

如果我想在我的 ASP.NET 应用程序中处理应用程序事件,我会在我的 Global.asax:

中注册一个处理程序
protected void Application_BeginRequest(object sender, EventArgs e)
{ ... }

Global.asax 已从 ASP.NET 中删除 5. 我现在如何处理此类事件?

ASP.NET 应用程序可以没有 global.asax。

HTTPModule 替代 global.asax。

阅读更多here

运行 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 class:

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

.

请看这里

对于任何应用程序启动级别调用,请参阅 application startup documentation