分类:
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-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256
50010702506256
欢迎加群交流技术