diff --git a/Apewer/Web/ApiProcessor.cs b/Apewer/Web/ApiProcessor.cs index 775145e..674ffc7 100644 --- a/Apewer/Web/ApiProcessor.cs +++ b/Apewer/Web/ApiProcessor.cs @@ -1,8 +1,6 @@ using Apewer.Network; -using Apewer.Source; using System; using System.Collections.Generic; -using System.Data; using System.Net; using System.Reflection; using static Apewer.Web.ApiUtility; @@ -47,8 +45,32 @@ namespace Apewer.Web response.Function = request.Function; _context.Response = response; - // 调用 API。 - Invoke(); + // OPTIONS + if (_context.Request.Method == HttpMethod.OPTIONS) + { + if (!_context.Options.AllowOptions) + { + _context.Response.Model = new ApiTextModel(""); + return; + } + } + + // 中间件 + var middlewares = _context.Invoker.Middlewares; + if (middlewares.Length > 0) + { + // 设置队列和回调。 + _mw_queue = new Queue(middlewares); + _context.SetMiddlewareCallback(MiddlewareNext); + + // 执行。 + MiddlewareNext(_context); + } + else + { + // 无中间件。 + Route(); + } } catch (Exception ex) { @@ -131,23 +153,6 @@ namespace Apewer.Web return null; } - // 寻找入口。 - void Invoke() - { - // OPTIONS - if (_context.Request.Method == HttpMethod.OPTIONS) - { - if (!_context.Options.AllowOptions) - { - _context.Response.Model = new ApiTextModel(""); - return; - } - } - - // 中间件 - InvokeMiddwares(); - } - #endregion #region common @@ -278,24 +283,6 @@ namespace Apewer.Web middleware.Invoke(context); } - // 调用中间件。 - void InvokeMiddwares() - { - var types = _context.Invoker.Middlewares; - if (types.Length < 1) - { - Route(); - return; - } - - // 设置队列和回调。 - _mw_queue = new Queue(types); - _context.SetMiddlewareCallback(MiddlewareNext); - - // 执行。 - MiddlewareNext(_context); - } - // 执行路由。 void Route() { @@ -308,7 +295,7 @@ namespace Apewer.Web if (action != null) { _context.ApiAction = action; - Invoke(action); + InvokeAction(action); return; } } @@ -318,7 +305,7 @@ namespace Apewer.Web { var appName = _context.Request.Application; var application = _context.Entries.GetApplication(appName); - Invoke(application); + InvokeApplication(application); return; } @@ -332,7 +319,7 @@ namespace Apewer.Web #region route // 执行 Action。 - void Invoke(ApiAction action) + void InvokeAction(ApiAction action) { var controller = null as ApiController; try @@ -375,7 +362,7 @@ namespace Apewer.Web #region reflection // 创建控制器。 - void Invoke(ApiApplication application) + void InvokeApplication(ApiApplication application) { var options = _context.Options; var entries = _context.Entries; @@ -401,7 +388,7 @@ namespace Apewer.Web try { controller = CreateController(@default, _context); - Invoke(controller, application, null, options, request, response); + InvokeFunction(controller, application, null, options, request, response); } catch (Exception ex) { @@ -421,7 +408,7 @@ namespace Apewer.Web try { controller = CreateController(application.Type, _context); - Invoke(controller, application, function, options, request, response); + InvokeFunction(controller, application, function, options, request, response); } catch (Exception ex) { @@ -435,7 +422,7 @@ namespace Apewer.Web } // 调用 Function。 - void Invoke(ApiController controller, ApiApplication application, ApiFunction function, ApiOptions options, ApiRequest request, ApiResponse response) + void InvokeFunction(ApiController controller, ApiApplication application, ApiFunction function, ApiOptions options, ApiRequest request, ApiResponse response) { try {