From 8bbdf2f2cc351eea57801cde3f9b0598a8414320 Mon Sep 17 00:00:00 2001 From: Elivo Date: Tue, 15 Jul 2025 23:31:17 +0800 Subject: [PATCH] =?UTF-8?q?ApiController=20=E5=A2=9E=E5=8A=A0=20Context=20?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer/Web/ApiContext.cs | 3 +++ Apewer/Web/ApiController.cs | 9 +++++++-- Apewer/Web/ApiProcessor.cs | 10 +++++----- Apewer/Web/ApiUtility.cs | 32 ++++++++++++++------------------ 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/Apewer/Web/ApiContext.cs b/Apewer/Web/ApiContext.cs index 424b875..63d1c94 100644 --- a/Apewer/Web/ApiContext.cs +++ b/Apewer/Web/ApiContext.cs @@ -71,6 +71,9 @@ namespace Apewer.Web } + /// 自定义数据。若此自定义数据实现了 ,将会与 Context 一起自动释放。 + public object Data { get; set; } + } } diff --git a/Apewer/Web/ApiController.cs b/Apewer/Web/ApiController.cs index 630c3c9..1a691a8 100644 --- a/Apewer/Web/ApiController.cs +++ b/Apewer/Web/ApiController.cs @@ -8,16 +8,21 @@ namespace Apewer.Web public abstract class ApiController { + internal ApiContext _context = null; + internal Func _func = null; internal Action _action = null; internal Action _default = null; internal ApiOptions _options = null; + /// 获取 API 上下文。 + public virtual ApiContext Context { get => _context; } + /// 获取 API 请求模型。 - public ApiRequest Request { get; internal set; } + public virtual ApiRequest Request { get => _context?.Request; } /// 获取 API 响应模型。 - public ApiResponse Response { get; internal set; } + public virtual ApiResponse Response { get => _context?.Response; } /// 创建控制器实例。可通过初始化程序返回布尔值,以执行 Function 解析。 /// 初始化程序。当返回 False 时等同于声明 Independent,将不再匹配 Function 和 Default。 diff --git a/Apewer/Web/ApiProcessor.cs b/Apewer/Web/ApiProcessor.cs index b94e7f5..7ae4493 100644 --- a/Apewer/Web/ApiProcessor.cs +++ b/Apewer/Web/ApiProcessor.cs @@ -161,10 +161,10 @@ namespace Apewer.Web static Type Void = typeof(void); // 创建控制器实例 - static ApiController CreateController(Type type, ApiRequest request, ApiResponse response, ApiOptions options) + static ApiController CreateController(Type type, ApiContext context) { var controller = (ApiController)Activator.CreateInstance(type); - ApiUtility.SetProperties(controller, request, response, options); + ApiUtility.SetContext(controller, context); return controller; } @@ -271,7 +271,7 @@ namespace Apewer.Web try { // 准备控制器。 - controller = CreateController(action.Type, _context.Request, _context.Response, _context.Options); + controller = CreateController(action.Type, _context); // 准备参数。 var parameters = action.Parameters; @@ -333,7 +333,7 @@ namespace Apewer.Web var controller = null as ApiController; try { - controller = CreateController(@default, request, response, options); + controller = CreateController(@default, _context); Invoke(controller, application, null, options, request, response); } catch (Exception ex) @@ -353,7 +353,7 @@ namespace Apewer.Web var controller = null as ApiController; try { - controller = CreateController(application.Type, request, response, options); + controller = CreateController(application.Type, _context); Invoke(controller, application, function, options, request, response); } catch (Exception ex) diff --git a/Apewer/Web/ApiUtility.cs b/Apewer/Web/ApiUtility.cs index 0e56a23..4e8c8d9 100644 --- a/Apewer/Web/ApiUtility.cs +++ b/Apewer/Web/ApiUtility.cs @@ -438,13 +438,13 @@ namespace Apewer.Web #region ApiController - /// 设置控制器属性。 - public static void SetProperties(ApiController controller, ApiRequest request, ApiResponse response, ApiOptions options) + /// 设置控制器的 属性。 + /// + public static void SetContext(ApiController controller, ApiContext context) { - if (controller == null) return; - controller.Request = request; - controller.Response = response; - controller._options = options; + if (controller == null) throw new ArgumentNullException(nameof(controller)); + if (context == null) throw new ArgumentNullException(nameof(context)); + controller._context = context; } /// 获取由控制器构造函数指定的初始化程序。 @@ -453,9 +453,6 @@ namespace Apewer.Web /// 获取由控制器构造函数指定的默认程序。 public static Action GetDefault(this ApiController controller) => controller == null ? null : controller._default; - /// 获取选项。 - public static ApiOptions GetOptions(this ApiController controller) => controller == null ? null : controller._options; - /// 以 POST 转移请求到其它 URL。 private static string Transfer(ApiController controller, string url, string application = null, string function = null) { @@ -499,15 +496,14 @@ namespace Apewer.Web return null; } - /// 创建指定类型的控制器,并引用当前控制器的 Request 和 Response。 - public static T Create(this ApiController current) where T : ApiController, new() + /// 创建指定类型的控制器,并引用参照控制器的 Context。 + /// + public static T Create(this ApiController reference) where T : ApiController, new() { + if (reference == null) throw new ArgumentNullException(nameof(reference)); + var controller = new T(); - if (current != null) - { - controller.Request = current.Request; - controller.Response = current.Response; - } + controller._context = reference.Context; return controller; } @@ -515,7 +511,7 @@ namespace Apewer.Web public static void UseDefault(this ApiController current) { if (current == null) return; - var options = GetOptions(current); + var options = current._context?.Options; if (options == null) return; var type = options.Default; if (type == null) return; @@ -523,7 +519,7 @@ namespace Apewer.Web try { controller = (ApiController)Activator.CreateInstance(type); - SetProperties(controller, current.Request, current.Response, options); + SetContext(controller, current.Context); } catch {