Skip to content

Instantly share code, notes, and snippets.

@GuyHarwood
Last active December 16, 2015 16:08
Show Gist options
  • Save GuyHarwood/5460601 to your computer and use it in GitHub Desktop.
Save GuyHarwood/5460601 to your computer and use it in GitHub Desktop.
Returning the location of a newly created resource using Request Uri.
//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