public override void OnException(HttpActionExecutedContext actionExecutedContext) { var exceptionType = actionExecutedContext.Exception.GetType().Name; ReturnData returnData; switch (exceptionType) { case "ObjectNotFoundException": returnData = new ReturnData(HttpStatusCode.NotFound, actionExecutedContext.Exception.Message, "Error"); break; default: returnData = new ReturnData(HttpStatusCode.InternalServerError, "An error occurred, please try again or contact the administrator.", "Error"); break; } actionExecutedContext.Response = new HttpResponseMessage(returnData.HttpStatusCode) { Content = new StringContent(returnData.Content), ReasonPhrase = returnData.ReasonPhrase }; }
public override void OnException(ExceptionContext exceptionContext) { var exceptionType = exceptionContext.Exception.GetType().Name; ReturnData returnData; switch (exceptionType) { case "ObjectNotFoundException": returnData = new ReturnData(HttpStatusCode.NotFound, exceptionContext.Exception.Message, "Error"); break; default: returnData = new ReturnData(HttpStatusCode.InternalServerError, "An error occurred, please try again or contact the administrator.", "Error"); break; } exceptionContext.Controller.ViewData.Model = returnData.Content; exceptionContext.HttpContext.Response.StatusCode = (int)returnData.HttpStatusCode; exceptionContext.Result = new ViewResult { ViewName = "Error", ViewData = exceptionContext.Controller.ViewData }; exceptionContext.ExceptionHandled = true; base.OnException(exceptionContext); }