using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; using System.Web.Http.Filters; using System.Net.Http; using System.Web.Http.ExceptionHandling; using System.Web.Http; using System.Threading; using System.Threading.Tasks; using System.Net; namespace IStation.WebApi { /// /// /// public class CustomHandleErrorAttribute : ExceptionFilterAttribute { /// /// 自定义错误 /// /// public override void OnException(HttpActionExecutedContext actionExecutedContext) { IStation.Dto.ApiResult result = null; if (actionExecutedContext.Exception is NotImplementedException) { IStation.LogHelper.WriteError("NotImplementedException," + actionExecutedContext.Exception.Message); result = new IStation.Dto.ApiResult(IStation.Dto.ApiResultCode.Error, actionExecutedContext.Exception.Message); } else if (actionExecutedContext.Exception is TimeoutException) { IStation.LogHelper.WriteError("TimeoutException," + actionExecutedContext.Exception.Message); result = new IStation.Dto.ApiResult(IStation.Dto.ApiResultCode.TimeOut, actionExecutedContext.Exception.Message); } else { IStation.LogHelper.WriteError( string.Format("UnknowException,Exception:{0},StackTrace:{1}", actionExecutedContext.Exception.Message, actionExecutedContext.Exception.StackTrace)); result = new IStation.Dto.ApiResult(IStation.Dto.ApiResultCode.Error, actionExecutedContext.Exception.Message); } actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(result); base.OnException(actionExecutedContext); } } }