Beispiel #1
0
        /// <summary>
        /// 处理请求
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        protected virtual async Task HandlerRequestAsync(IChannelHandlerContext ctx, IFullHttpRequest request)
        {
            IFullHttpResponse response = null;

            IFilter[] globalFilters = _controllerBus.GetGlobalFilters();
            if (!request.Uri.StartsWith("/api/"))
            {
                if (CanNext)
                {
                    return;
                }
                response = HttpResponseHelper.GetHttpResponse(HttpResponseStatus.NotFound);
            }
            else
            {
                string[] urls = request.Uri.Split('/');
                if (urls.Length < 4)
                {
                    return;
                }
                string controllerKey = urls[2];
                try
                {
                    BaseController baseController = _controllerBus.GetController(controllerKey);
                    baseController.Request = request;
                    string     actionKey = urls[3].Split('?')[0];
                    ActionInfo action    = baseController.GetAction(actionKey);
                    if (!HandlerOptions(ctx, request, action, ref response))
                    {
                        response = await baseController.HandlerControllerBeforeFilterAsync(globalFilters);

                        if (response.Status.Code == HttpResponseStatus.OK.Code)
                        {
                            response = await GetResponseAsync(request, baseController, action, globalFilters);

                            await baseController.HandlerControllerAfterFilterAsync(response, globalFilters);
                        }
                    }
                }
                catch (MateralDotNettyServerException exception)
                {
                    ResultModel resultModel = ResultModel.Fail(exception.Message);
                    response = HttpResponseHelper.GetHttpResponse(HttpResponseStatus.NotFound, resultModel.ToJson());
                }
            }
            await SendHttpResponseAsync(ctx, request, response);

            StopHandler();
        }
Beispiel #2
0
        /// <summary>
        /// 处理请求
        /// </summary>
        private async Task HandlerRequestAsync(IChannelHandlerContext ctx, IFullHttpRequest request)
        {
            if (!request.Uri.StartsWith("/api/"))
            {
                return;
            }
            var urls          = request.Uri.Split('/');
            var controllerKey = urls[2];
            var controller    = _controllerBus.GetController(controllerKey);
            var actionKey     = urls[3].Split('?')[0];
            var action        = controller.GetAction(actionKey);

            //处理业务
            IFullHttpResponse response = await GetResponseAsync(request, controller, action);

            await SendHttpResponseAsync(ctx, response);

            StopHandler();
        }