Browse Source

ApiController 增加 Context 属性。

master
王厅 3 days ago
parent
commit
8bbdf2f2cc
  1. 3
      Apewer/Web/ApiContext.cs
  2. 9
      Apewer/Web/ApiController.cs
  3. 10
      Apewer/Web/ApiProcessor.cs
  4. 32
      Apewer/Web/ApiUtility.cs

3
Apewer/Web/ApiContext.cs

@ -71,6 +71,9 @@ namespace Apewer.Web
} }
/// <summary>自定义数据。若此自定义数据实现了 <see cref="IDisposable" />,将会与 Context 一起自动释放。</summary>
public object Data { get; set; }
} }
} }

9
Apewer/Web/ApiController.cs

@ -8,16 +8,21 @@ namespace Apewer.Web
public abstract class ApiController public abstract class ApiController
{ {
internal ApiContext _context = null;
internal Func<ApiController, bool> _func = null; internal Func<ApiController, bool> _func = null;
internal Action<ApiController> _action = null; internal Action<ApiController> _action = null;
internal Action<ApiController> _default = null; internal Action<ApiController> _default = null;
internal ApiOptions _options = null; internal ApiOptions _options = null;
/// <summary>获取 API 上下文。</summary>
public virtual ApiContext Context { get => _context; }
/// <summary>获取 API 请求模型。</summary> /// <summary>获取 API 请求模型。</summary>
public ApiRequest Request { get; internal set; } public virtual ApiRequest Request { get => _context?.Request; }
/// <summary>获取 API 响应模型。</summary> /// <summary>获取 API 响应模型。</summary>
public ApiResponse Response { get; internal set; } public virtual ApiResponse Response { get => _context?.Response; }
/// <summary>创建控制器实例。可通过初始化程序返回布尔值,以执行 Function 解析。</summary> /// <summary>创建控制器实例。可通过初始化程序返回布尔值,以执行 Function 解析。</summary>
/// <param name="initializer">初始化程序。当返回 False 时等同于声明 Independent,将不再匹配 Function 和 Default。</param> /// <param name="initializer">初始化程序。当返回 False 时等同于声明 Independent,将不再匹配 Function 和 Default。</param>

10
Apewer/Web/ApiProcessor.cs

@ -161,10 +161,10 @@ namespace Apewer.Web
static Type Void = typeof(void); 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); var controller = (ApiController)Activator.CreateInstance(type);
ApiUtility.SetProperties(controller, request, response, options); ApiUtility.SetContext(controller, context);
return controller; return controller;
} }
@ -271,7 +271,7 @@ namespace Apewer.Web
try try
{ {
// 准备控制器。 // 准备控制器。
controller = CreateController(action.Type, _context.Request, _context.Response, _context.Options); controller = CreateController(action.Type, _context);
// 准备参数。 // 准备参数。
var parameters = action.Parameters; var parameters = action.Parameters;
@ -333,7 +333,7 @@ namespace Apewer.Web
var controller = null as ApiController; var controller = null as ApiController;
try try
{ {
controller = CreateController(@default, request, response, options); controller = CreateController(@default, _context);
Invoke(controller, application, null, options, request, response); Invoke(controller, application, null, options, request, response);
} }
catch (Exception ex) catch (Exception ex)
@ -353,7 +353,7 @@ namespace Apewer.Web
var controller = null as ApiController; var controller = null as ApiController;
try try
{ {
controller = CreateController(application.Type, request, response, options); controller = CreateController(application.Type, _context);
Invoke(controller, application, function, options, request, response); Invoke(controller, application, function, options, request, response);
} }
catch (Exception ex) catch (Exception ex)

32
Apewer/Web/ApiUtility.cs

@ -438,13 +438,13 @@ namespace Apewer.Web
#region ApiController #region ApiController
/// <summary>设置控制器属性。</summary> /// <summary>设置控制器的 <see cref="ApiController.Context" /> 属性。</summary>
public static void SetProperties(ApiController controller, ApiRequest request, ApiResponse response, ApiOptions options) /// <exception cref="ArgumentNullException" />
public static void SetContext(ApiController controller, ApiContext context)
{ {
if (controller == null) return; if (controller == null) throw new ArgumentNullException(nameof(controller));
controller.Request = request; if (context == null) throw new ArgumentNullException(nameof(context));
controller.Response = response; controller._context = context;
controller._options = options;
} }
/// <summary>获取由控制器构造函数指定的初始化程序。</summary> /// <summary>获取由控制器构造函数指定的初始化程序。</summary>
@ -453,9 +453,6 @@ namespace Apewer.Web
/// <summary>获取由控制器构造函数指定的默认程序。</summary> /// <summary>获取由控制器构造函数指定的默认程序。</summary>
public static Action<ApiController> GetDefault(this ApiController controller) => controller == null ? null : controller._default; public static Action<ApiController> GetDefault(this ApiController controller) => controller == null ? null : controller._default;
/// <summary>获取选项。</summary>
public static ApiOptions GetOptions(this ApiController controller) => controller == null ? null : controller._options;
/// <summary>以 POST 转移请求到其它 URL。</summary> /// <summary>以 POST 转移请求到其它 URL。</summary>
private static string Transfer(ApiController controller, string url, string application = null, string function = null) private static string Transfer(ApiController controller, string url, string application = null, string function = null)
{ {
@ -499,15 +496,14 @@ namespace Apewer.Web
return null; return null;
} }
/// <summary>创建指定类型的控制器,并引用当前控制器的 Request 和 Response。</summary> /// <summary>创建指定类型的控制器,并引用参照控制器的 Context。</summary>
public static T Create<T>(this ApiController current) where T : ApiController, new() /// <exception cref="ArgumentNullException" />
public static T Create<T>(this ApiController reference) where T : ApiController, new()
{ {
if (reference == null) throw new ArgumentNullException(nameof(reference));
var controller = new T(); var controller = new T();
if (current != null) controller._context = reference.Context;
{
controller.Request = current.Request;
controller.Response = current.Response;
}
return controller; return controller;
} }
@ -515,7 +511,7 @@ namespace Apewer.Web
public static void UseDefault(this ApiController current) public static void UseDefault(this ApiController current)
{ {
if (current == null) return; if (current == null) return;
var options = GetOptions(current); var options = current._context?.Options;
if (options == null) return; if (options == null) return;
var type = options.Default; var type = options.Default;
if (type == null) return; if (type == null) return;
@ -523,7 +519,7 @@ namespace Apewer.Web
try try
{ {
controller = (ApiController)Activator.CreateInstance(type); controller = (ApiController)Activator.CreateInstance(type);
SetProperties(controller, current.Request, current.Response, options); SetContext(controller, current.Context);
} }
catch catch
{ {

Loading…
Cancel
Save