Skip to content

Instantly share code, notes, and snippets.

@sdanna
Created December 9, 2013 17:15
Show Gist options
  • Save sdanna/7876150 to your computer and use it in GitHub Desktop.
Save sdanna/7876150 to your computer and use it in GitHub Desktop.
How to convert a web api version 1 HttpResponseMessage based code base to a web api version 2 IHttpActionResult based code base. (http://weblogs.asp.net/dwahlin/archive/2013/11/11/new-features-in-asp-net-web-api-2-part-i.aspx)
public IHttpActionResult Post([FromBody]Customer cust)
{
var newCust = _Repository.InsertCustomer(cust);
if (newCust != null)
{
return Created<Customer>(Request.RequestUri + newCust.ID.ToString(), newCust);
}
else
{
return Conflict();
}
}
public HttpResponseMessage Post([FromBody]Customer cust)
{
var newCust = _Repository.InsertCustomer(cust);
if (newCust != null)
{
var msg = new HttpResponseMessage(HttpStatusCode.Created);
msg.Headers.Location = new Uri(Request.RequestUri + newCust.ID.ToString());
return msg;
}
else
{
throw new HttpResponseException(HttpStatusCode.Conflict);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment