ASP.NET Core中间件库
Go to file
2019-12-11 17:20:55 +08:00
Falcon.Middleware 修改了一下包说明 2019-12-11 14:26:28 +08:00
Falcon.Middleware.Test 基类InvokeNextBefore和InvokeNextAfter方法改为virtual方法,这样子类不必重写所有两个方法 2019-12-11 14:20:31 +08:00
.gitignore 添加中间件基类,和测试项目 2019-12-11 13:58:31 +08:00
Falcon.Middleware.sln 添加中间件基类,和测试项目 2019-12-11 13:58:31 +08:00
README.MD 更新 'README.MD' 2019-12-11 17:20:55 +08:00

ASP.NET CORE中间件相关扩展。

安装

中间件基类MiddlewareBase

可以自行继承并实现自己的中间件类。例如:

    /// <summary>
    /// 测试中间件
    /// </summary>
    public class MiddlewareTest:MiddlewareBase
    {
        public MiddlewareTest(RequestDelegate next) : base(next) {
        }

        /// <summary>
        /// 在调用next之后执行
        /// </summary>
        /// <param name="context">请求上下文</param>
        public async override void InvokeNextAfter(HttpContext context) {
            await context.Response.WriteAsync("\nInvokeNextAfter");
        }
        /// <summary>
        /// 在调用next之前执行
        /// </summary>
        /// <param name="context">请求上下文</param>
        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<MiddlewareTest>();

  app.UseEndpoints(endpoints => {
    endpoints.MapGet("/",async context => {
      await context.Response.WriteAsync("Hello World!");
    });
  });
}

浏览器发送请求后:

InvokeNextBefore
Hello World!
InvokeNextAfter