在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
.