public override HttpActionDescriptor SelectAction(HttpControllerContext controllerContext)
        {
            HttpActionDescriptor decriptor = null;

            try
            {
                decriptor = base.SelectAction(controllerContext);
            }
            catch (HttpResponseException ex)
            {
                var code   = ex.Response.StatusCode;
                var result = new EWorkResultInfo
                {
                    Code   = 10006,
                    Entity = ex.Response.Content.ReadAsAsync <object>().Result
                };
                if (code == HttpStatusCode.NotFound || code == HttpStatusCode.MethodNotAllowed)
                {
                    ex.Response.Content = new ObjectContent(typeof(EWorkResultInfo), result, GlobalConfiguration.Configuration.Formatters.JsonFormatter);
                }

                throw;
            }

            return(decriptor);
        }
Example #2
0
        public override Task <HttpResponseMessage> InvokeActionAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            var responseMessage = base.InvokeActionAsync(actionContext, cancellationToken);

            if (responseMessage.Exception != null)
            {
                var baseException = responseMessage.Exception.InnerExceptions[0];

                var result = new EWorkResultInfo
                {
                    Message = baseException.Message, //"服务器内部错误",
                    Code    = 10001,
                    Entity  = responseMessage.Exception
                };

                if (baseException is TimeoutException)
                {
                    result.Code = 10004;
                    //result.Message = "任务超时";
                }

                return(Task.Run(() => new HttpResponseMessage()
                {
                    Content = new ObjectContent(typeof(EWorkResultInfo), result, GlobalConfiguration.Configuration.Formatters.JsonFormatter)
                }, cancellationToken));
            }
            return(responseMessage);
        }