public class ExceptionFilter : System.Web.Http.Filters.IExceptionFilter { bool IFilter.AllowMultiple => true; Task IExceptionFilter.ExecuteExceptionFilterAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) { var ex = actionExecutedContext.Exception; actionExecutedContext.Response = new System.Net.Http.HttpResponseMessage() ; actionExecutedContext.Response.Content = new System.Net.Http.ObjectContent(typeof(ReplyPack<string>), new ReplyPack<string> { Status = 500, Msg = ex.Message, Value = null }, new JsonMediaTypeFormatter()); return Task.FromResult(0); } }
config.Filters.Add(new ExceptionFilter());