You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
2.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using System;
  2. namespace Apewer.Web
  3. {
  4. /// <summary>WebAPI 控制器基类。</summary>
  5. /// <remarks>控制器具有 Independent 特性时,不匹配 Function,且忽略 Initializer 返回值。</remarks>
  6. public abstract class ApiController
  7. {
  8. internal Func<ApiController, bool> _func = null;
  9. internal Action<ApiController> _action = null;
  10. internal Action<ApiController> _default = null;
  11. internal ApiOptions _options = null;
  12. /// <summary>获取 API 请求模型。</summary>
  13. public ApiRequest Request { get; internal set; }
  14. /// <summary>获取 API 响应模型。</summary>
  15. public ApiResponse Response { get; internal set; }
  16. /// <summary>创建控制器实例。可通过初始化程序返回布尔值,以执行 Function 解析。</summary>
  17. /// <param name="initializer">初始化程序。当返回 False 时等同于声明 Independent,将不再匹配 Function 和 Default。</param>
  18. /// <param name="default">在匹配 Function 且失败后执行的方法。</param>
  19. /// <remarks>执行顺序:构造函数 -> Initializer -> Function -> Default -> Dispose</remarks>
  20. public ApiController(Func<ApiController, bool> initializer = null, Action<ApiController> @default = null)
  21. {
  22. _func = initializer;
  23. _default = @default;
  24. }
  25. /// <summary>创建控制器实例。可通过初始化程序返回布尔值,以执行 Function 解析。</summary>
  26. /// <param name="initializer">初始化程序。</param>
  27. /// <param name="default">在匹配 Function 且失败后执行的方法。</param>
  28. /// <remarks>执行顺序:构造函数 -> Initializer -> Function -> Default -> Dispose</remarks>
  29. public ApiController(Action<ApiController> initializer, Action<ApiController> @default = null)
  30. {
  31. _func = (c) =>
  32. {
  33. initializer?.Invoke(this);
  34. return true;
  35. };
  36. _default = @default;
  37. }
  38. }
  39. }