From a0f3d367f00838a17d19167b11d3a76a332f1d65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E9=95=BF=E5=BE=81?= Date: Wed, 11 Dec 2019 14:14:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0readme.md=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.MD | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 README.MD diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..e19cd6a --- /dev/null +++ b/README.MD @@ -0,0 +1,55 @@ +ASP.NET CORE中间件相关扩展。 + +中间件基类`MiddlewareBase` + +可以自行继承并实现自己的中间件类。例如: +``` + /// + /// 测试中间件 + /// + public class MiddlewareTest:MiddlewareBase + { + public MiddlewareTest(RequestDelegate next) : base(next) { + } + + /// + /// 在调用next之后执行 + /// + /// 请求上下文 + public async override void InvokeNextAfter(HttpContext context) { + await context.Response.WriteAsync("\nInvokeNextAfter"); + } + /// + /// 在调用next之前执行 + /// + /// 请求上下文 + public async override void InvokeNextBefore(HttpContext context) { + await context.Response.WriteAsync("InvokeNextBefore\n"); + } + } + +``` +然后修改setup.cs +``` +public void Configure(IApplicationBuilder app,IWebHostEnvironment env) { + if(env.IsDevelopment()) { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseMiddleware(); + + app.UseEndpoints(endpoints => { + endpoints.MapGet("/",async context => { + await context.Response.WriteAsync("Hello World!"); + }); + }); +} +``` +浏览器发送请求后: +``` +InvokeNextBefore +Hello World! +InvokeNextAfter +``` \ No newline at end of file