Skip to content

Instantly share code, notes, and snippets.

@GeoffCox
Created January 8, 2015 21:26
Show Gist options
  • Save GeoffCox/ea949a32f599cb8b3995 to your computer and use it in GitHub Desktop.
Save GeoffCox/ea949a32f599cb8b3995 to your computer and use it in GitHub Desktop.
Web API Error Attribute
// This attribute helps return exception text from web API methods when applied to a MVC Web API controller.
public class HandleWebApiErrorAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
var exception = context.Exception as Exception;
if (exception != null)
{
var message = exception.Message;
message = message.Replace('\r', ' ').Replace('\n', ' ');
var response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
response.Content = new StringContent(message);
response.ReasonPhrase = message;
context.Response = response;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment