Skip to content

Instantly share code, notes, and snippets.

@reidev275
Last active August 29, 2015 13:55
Show Gist options
  • Save reidev275/8743268 to your computer and use it in GitHub Desktop.
Save reidev275/8743268 to your computer and use it in GitHub Desktop.
ClientError
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");
}
}
}
public class DogsController : ApiController
{
public int Post(Dog dog)
{
if (dog == null) throw new ClientError(HttpStatusCode.BadRequest, "No dog provided to create");
...
}
}
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