diff --git a/Falcon.SugarApi/ApiDefinistions/Middleware/MiddlewareBase.cs b/Falcon.SugarApi/ApiDefinistions/Middleware/MiddlewareBase.cs new file mode 100644 index 0000000..9db9458 --- /dev/null +++ b/Falcon.SugarApi/ApiDefinistions/Middleware/MiddlewareBase.cs @@ -0,0 +1,53 @@ +using Microsoft.AspNetCore.Http; +using System.Threading.Tasks; + +namespace Falcon.SugarApi.ApiDefinistions.Middleware +{ + /// + /// ASP.NET CORE 中间件基类,模板类 + /// + public abstract class MiddlewareBase + { + /// + /// 下一步请求 + /// + private readonly RequestDelegate Next; + + /// + /// 构造中间件 + /// + /// 下一步 + protected MiddlewareBase(RequestDelegate next) { + Next = next; + } + + /// + /// 执行中间件 + /// + /// HttpContext + /// 任务 + public virtual async Task InvokeAsync(HttpContext context) { + await BeforeNext(context); + await this.Next(context); + await AfterNext(context); + } + + /// + /// 在下一步之前执行的代码。由InvokeAsync调用 + /// + /// HttpContext + /// 任务 + public virtual async Task BeforeNext(HttpContext context) { + await Task.CompletedTask; + } + + /// + /// 在下一步之后执行的代码。由InvokeAsync调用 + /// + /// HttpContext + /// 任务 + public virtual async Task AfterNext(HttpContext context) { + await Task.CompletedTask; + } + } +}