Created
May 11, 2016 20:47
-
-
Save Luis-Palacios/3c9cc49cd5c3733358f2e1619bb9fe0c to your computer and use it in GitHub Desktop.
Created Method for ASP.Net Web api
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
return CreatedAtRoute("DefaultApi", new { id = test.TestId }, test); | |
string url = Url.Link("DefaultApi", new { id = test.TestId }); | |
return Created(url, test); | |
//For Route Attirbutes | |
[Route("api/books/{id}", Name="GetBookById")] | |
public BookDto GetBook(int id) | |
{ | |
// Implementation not shown... | |
} | |
[Route("api/books")] | |
public HttpResponseMessage Post(Book book) | |
{ | |
// Validate and add book to database (not shown) | |
var response = Request.CreateResponse(HttpStatusCode.Created); | |
// Generate a link to the new book and set the Location header in the response. | |
string uri = Url.Link("GetBookById", new { id = book.BookId }); | |
response.Headers.Location = new Uri(uri); | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment