public override void OnActionExecuting(HttpActionContext actionContext)
        {
            string modelName;

            if (!canModelNull && IsModelNull(actionContext.ActionArguments, out modelName))
            {
                var back = new HttpBackResult <string> {
                    Status = (int)HttpBackCode.Parameter, Msg = $"参数\"{modelName}\"不可为空", Data = null
                };
                actionContext.Response         = new HttpResponseMessage(HttpStatusCode.OK);
                actionContext.Response.Content = new StringContent(back.ToJson());
                return;
            }

            if (!actionContext.ModelState.IsValid)
            {
                var back = new HttpBackResult <string> {
                    Status = (int)HttpBackCode.Parameter, Msg = actionContext.ModelState.GetErrorMessage(), Data = null
                };
                actionContext.Response         = new HttpResponseMessage(HttpStatusCode.OK);
                actionContext.Response.Content = new StringContent(back.ToJson());
            }
            else
            {
                base.OnActionExecuting(actionContext);
            }
        }
예제 #2
0
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            var back = new HttpBackResult <string> {
                Status = (int)HttpBackCode.Parameter, Msg = actionExecutedContext.Exception.Message, Data = null
            };

            actionExecutedContext.Response         = new HttpResponseMessage(HttpStatusCode.OK);
            actionExecutedContext.Response.Content = new StringContent(back.ToJson());
        }