public override void OnActionExecuting(HttpActionContext actionContext) { if (!actionContext.ModelState.IsValid) { var r = new ResultResHere() { httpcode = 500, message = "错了" }; //自定义错误信息 var item = actionContext.ModelState.Values.Take(1).SingleOrDefault(); r.message = item.Errors.Where(b => !string.IsNullOrWhiteSpace(b.ErrorMessage)).Take(1).SingleOrDefault().ErrorMessage; actionContext.Response = actionContext.Request.CreateResponse(System.Net.HttpStatusCode.OK, r); } }
public override void OnException(HttpActionExecutedContext actionExecutedContext) { ResultResHere r = new ResultResHere(); r.httpcode = 500; var test = actionExecutedContext.Exception.Message; if (isServer == "0") { r.message = test; } else { r.message = "网络不稳定"; } actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(System.Net.HttpStatusCode.OK, r); base.OnException(actionExecutedContext); }