定义
.Net Core 里面的中间件串联在一起组成了管道,所有的 Request 及 Response都会经过管道.
建立中间件
FirstMiddleware.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class TestMiddleware { private readonly RequestDelegate _next;
public TestMiddleware(RequestDelegate next) { _next = next; }
public async Task Invoke(HttpContext context) { await context.Response.WriteAsync($"TestMiddleware 进入. \r\n");
await _next(context);
await context.Response.WriteAsync($"TestMiddleware 退出. \r\n"); } }
|
全局注册
在Startup.Configue中注册,则全部的Request都会通过该中间件.
Startup.cs
1 2 3 4 5 6 7
| public class Startup { public void Configure(IApplicationBuilder app) { app.UseMiddleware<TestMiddleware>(); } }
|
区域注册
中间件也可以仅在特定的控制器和请求上注册.
ValueController.cs
1 2 3 4 5 6 7 8 9
| [MiddlewareFilter(typeof(TestMiddleware))] public class ValueController : Controller { [MiddlewareFilter(typeof(TestMiddleware))] public IActionResult Index() { } }
|
扩展方法注册
大部分的中间件都会用一个静态方法来进行注册.
CustomMiddlewareExtensions.cs
1 2 3 4 5 6 7
| public static class CustomMiddlewareExtensions { public static IApplicationBuilder UseTestMiddleware(this IApplicationBuilder builder) { return builder.UseMiddleware<TestMiddleware>(); } }
|
Startup.cs
1 2 3 4 5 6 7
| public class Startup { public void Configure(IApplicationBuilder app) { app.UseTestMiddleware(); } }
|