tnblog
首页
视频
资源
登录

重写基类的异常处理方法

3037人阅读 2020/3/31 15:30 总访问:342190 评论:0 收藏:0 手机
分类: ASP.NET
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);
        }


评价
当你知道迷惑时,并不可怜,当你不知道迷惑时,才是最可怜的。
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术