幽梦紫曦

重写基类的异常处理方法

电脑版发表于:2020/3/31 15:30
1.public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            //异常日志记录
            LogHelper.WriteError(string.Format("\r\nWebapi Global异常: Post数据:{0}\r\nHeaders:\r\n{1} URL:{2}",
            HttpContext.Current.Request.Form, 
            Global.GetHeaders(HttpContext.Current),
            HttpContext.Current?.Request?.Url?.ToString() ?? ""), actionExecutedContext.Exception);
            HttpResponseMessage response = new HttpResponseMessage();
            response.Content = new StringContent("{\"error\":1}");
            //返回调用方具体的异常信息
            if (actionExecutedContext.Exception is NotImplementedException)
            {
                response.StatusCode = HttpStatusCode.NotImplemented;
                actionExecutedContext.Response = response;
            }
            else if (actionExecutedContext.Exception is TimeoutException)
            {
                response.StatusCode = HttpStatusCode.NotImplemented;
                actionExecutedContext.Response = response;
            }
            //如果找不到相应的异常,统一返回服务端错误500
            else
            {
                response.StatusCode = HttpStatusCode.InternalServerError;
                actionExecutedContext.Response = response;
            }
            base.OnException(actionExecutedContext);
        }


关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢