Last active
August 29, 2015 13:55
-
-
Save reidev275/8743268 to your computer and use it in GitHub Desktop.
ClientError
This file contains 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
using Newtonsoft.Json; | |
using System.Net; | |
using System.Net.Http; | |
using System.Text; | |
using System.Web.Http; | |
namespace DocSol.Webservice.Controllers | |
{ | |
public class ClientError : HttpResponseException | |
{ | |
public ClientError(HttpStatusCode statusCode, object response) | |
: base(new HttpResponseMessage(statusCode) | |
{ | |
Content = GetJsonContent(response) | |
}) | |
{} | |
static StringContent GetJsonContent(object message) | |
{ | |
return new StringContent(JsonConvert.SerializeObject(message), Encoding.UTF8, "application/json"); | |
} | |
} | |
} |
This file contains 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
public class DogsController : ApiController | |
{ | |
public int Post(Dog dog) | |
{ | |
if (dog == null) throw new ClientError(HttpStatusCode.BadRequest, "No dog provided to create"); | |
... | |
} | |
} |
This file contains 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
public class DogsController : ApiController | |
{ | |
public int Post(Dog dog) | |
{ | |
if (dog == null) | |
throw new HttpResponseException( | |
Request.CreateResponse(HttpStatusCode.BadRequest, new { Message = "No dog provided to create" }) | |
); | |
... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment