Created
January 8, 2015 21:26
-
-
Save GeoffCox/ea949a32f599cb8b3995 to your computer and use it in GitHub Desktop.
Web API Error Attribute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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