Created
December 9, 2013 17:15
-
-
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)
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 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(); | |
} | |
} |
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 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