|
|
@ -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 |
|
|
|
{ |
|
|
|