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
{
internal ApiContext _context = null;
internal Func<ApiController, bool> _func = null;
internal Action<ApiController> _action = null;
internal Action<ApiController> _default = null;
internal ApiOptions _options = null;
/// <summary>获取 API 上下文。</summary>
public virtual ApiContext Context { get => _context; }
/// <summary>获取 API 请求模型。</summary>
public ApiRequest Request { get; internal set; }
public virtual ApiRequest Request { get => _context?.Request; }
/// <summary>获取 API 响应模型。</summary>
public ApiResponse Response { get; internal set; }
public virtual ApiResponse Response { get => _context?.Response; }
/// <summary>创建控制器实例。可通过初始化程序返回布尔值,以执行 Function 解析。</summary>
/// <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 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)

32
Apewer/Web/ApiUtility.cs

@ -438,13 +438,13 @@ namespace Apewer.Web
#region ApiController
/// <summary>设置控制器属性。</summary>
public static void SetProperties(ApiController controller, ApiRequest request, ApiResponse response, ApiOptions options)
/// <summary>设置控制器的 <see cref="ApiController.Context" /> 属性。</summary>
/// <exception cref="ArgumentNullException" />
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;
}
/// <summary>获取由控制器构造函数指定的初始化程序。</summary>
@ -453,9 +453,6 @@ namespace Apewer.Web
/// <summary>获取由控制器构造函数指定的默认程序。</summary>
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>
private static string Transfer(ApiController controller, string url, string application = null, string function = null)
{
@ -499,15 +496,14 @@ namespace Apewer.Web
return null;
}
/// <summary>创建指定类型的控制器,并引用当前控制器的 Request 和 Response。</summary>
public static T Create<T>(this ApiController current) where T : ApiController, new()
/// <summary>创建指定类型的控制器,并引用参照控制器的 Context。</summary>
/// <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();
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
{

Loading…
Cancel
Save