From 64facf909162954761b4ab0dac20b4b27ca9b4be Mon Sep 17 00:00:00 2001 From: Elivo Date: Sun, 30 Mar 2025 21:17:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20ApiController=EF=BC=8C?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E7=AE=80=E5=8C=96=E6=B4=BE=E7=94=9F=E7=B1=BB?= =?UTF-8?q?=E7=9A=84=E6=9E=84=E9=80=A0=E5=87=BD=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer/Web/ApiController.cs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Apewer/Web/ApiController.cs b/Apewer/Web/ApiController.cs index 0f24ce4..630c3c9 100644 --- a/Apewer/Web/ApiController.cs +++ b/Apewer/Web/ApiController.cs @@ -45,4 +45,35 @@ namespace Apewer.Web } + /// WebAPI 控制器基类。 + /// 控制器具有 Independent 特性时,不匹配 Function,且忽略 Initializer 返回值。 + public abstract class ApiController : ApiController where T : ApiController + { + + static Func ToBaseFunc(Func current) + { + if (current == null) return null; + return new Func(c => current.Invoke((T)c)); + } + + static Action ToBaseAction(Action current) + { + if (current == null) return null; + return new Action(c => current.Invoke((T)c)); + } + + /// 创建控制器实例。可通过初始化程序返回布尔值,以执行 Function 解析。 + /// 初始化程序。当返回 False 时等同于声明 Independent,将不再匹配 Function 和 Default。 + /// 在匹配 Function 且失败后执行的方法。 + /// 执行顺序:构造函数 -> Initializer -> Function -> Default -> Dispose + public ApiController(Func initializer = null, Action @default = null) : base(ToBaseFunc(initializer), ToBaseAction(@default)) { } + + /// 创建控制器实例。可通过初始化程序返回布尔值,以执行 Function 解析。 + /// 初始化程序。 + /// 在匹配 Function 且失败后执行的方法。 + /// 执行顺序:构造函数 -> Initializer -> Function -> Default -> Dispose + public ApiController(Action initializer, Action @default = null) : base(ToBaseAction(initializer), ToBaseAction(@default)) { } + + } + }