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)) { } + + } + }