public class WebApiExceptionFitlerAttribute: ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
var tBaseResult = new TBaseResult<object>();
tBaseResult.IsSuccess = false;
if (actionExecutedContext.Exception is BussinessException)
{
tBaseResult.Code = 200;
tBaseResult.Message = actionExecutedContext.Exception.Message;
}
else if (actionExecutedContext.Exception is AuthorizationException)
{
tBaseResult.Code = 403;
tBaseResult.Message = actionExecutedContext.Exception.Message;
}
else if (actionExecutedContext.Exception is AuthenticationException)
{
tBaseResult.Code = 401;
tBaseResult.Message = actionExecutedContext.Exception.Message;
}
else
{
tBaseResult.Code = 500;
tBaseResult.Message = actionExecutedContext.Exception.Message;
}
var tBaseResultJson = Newtonsoft.Json.JsonConvert.SerializeObject(tBaseResult);
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(tBaseResultJson, Encoding.GetEncoding("UTF-8"), "application/json") };
actionExecutedContext.Response = result;
base.OnException(actionExecutedContext);
}
}