Last active
December 16, 2015 16:08
-
-
Save GuyHarwood/5460601 to your computer and use it in GitHub Desktop.
Returning the location of a newly created resource using Request Uri.
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
//Taken from http://codebetter.com/glennblock/2012/05/24/two-ways-to-work-with-http-responses-in-apicontroller-httpresponsemessage-and-httpresponseexception/ | |
public class CustomerController : ApiController | |
{ | |
private ICustomerContext repo; | |
public CustomerController(ICustomerContext repo) | |
{ | |
this.repo = repo; | |
} | |
public HttpResponseMessage Post(Customer customer) | |
{ | |
repo.Add(customer); | |
repo.SaveChanges(); | |
var response = Request.CreateResponse(HttpStatusCode.Created, customer); | |
response.Headers.Location = new Uri(Request.RequestUri, string.format("customer/{0}", customer.id)); | |
return response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment