WebApi 异常处理解决方案

1.继承ExceptionFilterAttribute类,重写OnException方法

 public class WebApiExceptionFilterAttribute : ExceptionFilterAttribute
    {
        //重写基类的异常处理方法
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            //业务异常处理
            if (actionExecutedContext.Exception is BusinessException)
            {
                var businessException = (BusinessException)actionExecutedContext.Exception;
                //自定义业务异常编码
                int businessExceptionCode = businessException.ErrorMessage.Code;
                //业务异常消息
                string businessExceptionMsg = businessException.Message;
                //异常消息Json串
                var err = new { errcode = businessExceptionCode, errmsg = businessExceptionMsg };
                string errMsg = JsonConvert.SerializeObject(err);

                //系统异常码
                var oResponse = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                oResponse.Content = new StringContent(" ");
                oResponse.Headers.Add("errMsg", errMsg);
                actionExecutedContext.Response = oResponse;
            }
            else
            {
                actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                //异常消息Json串
                var err = new { errcode = CommonErrorCode.ServiceError, errmsg = actionExecutedContext.Exception.Message };
                string errMsg = JsonConvert.SerializeObject(err);

                //系统异常码
                var oResponse = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                oResponse.Content = new StringContent(" ");
                oResponse.Headers.Add("errMsg", errMsg);
                actionExecutedContext.Response = oResponse;
            }

            base.OnException(actionExecutedContext);
        }
    }

 2. Global.asax中,Application_Start方法中添加过滤器

protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);
            GlobalConfiguration.Configuration.Filters.Add(new WebApiExceptionFilterAttribute());
        }

 3.例子:

控制器:

public class TestController : ApiController
    {
        [HttpGet]
        public string GetTest1()
        {
            
            return "value1";
        }

        [HttpGet]
        public string GetTest2()
        {
            throw new BusinessException(123456,"业务异常");
        }

        [HttpGet]
        public string GetTest3()
        {
            throw new Exception("系统异常");
        }
    }

 结果:

提升:

为了满足每个webapi项目都应用自定义异常,把异常类封装到程序集中使用

1.结构:

问题:YesWay.WebAPI异常抛出类库添加引用需与webapI项目用的引用需相同,

之前的这些我都是在引用管理器,程序集中一个个找的,添加的,出现进不了OnException方法的问题,用nuget添加的就好了,可能是版本不一样吧

具体有这些东东:(他们之间有依赖,不需要一个个加,加一个可能会出来好几个...)

webapi需要引用的包:
Microsoft.AspNet.WebApi
Microsoft.AspNet.WebApi.Client
Microsoft.AspNet.WebApi.Client.zh-Hans
Microsoft.AspNet.WebApi.Core
Microsoft.AspNet.WebApi.Core.zh-Hans
Microsoft.AspNet.WebApi.WebHost
Microsoft.AspNet.WebApi.WebHost.zh-Hans
Microsoft.CodeDom.Providers.DotNetCompilerPlat
Microsoft.Net.Compilers
Newtonsoft.Json

2.webapi项目引用程序集

3.可以在控制器,控制器方法加特性使用

global中加这句,所有的控制器就都加上了
 
 
原文地址:https://www.cnblogs.com/liuqiyun/p/9151519.html