Skip to content

Instantly share code, notes, and snippets.

@Luis-Palacios
Created May 11, 2016 20:47
Show Gist options
  • Save Luis-Palacios/3c9cc49cd5c3733358f2e1619bb9fe0c to your computer and use it in GitHub Desktop.
Save Luis-Palacios/3c9cc49cd5c3733358f2e1619bb9fe0c to your computer and use it in GitHub Desktop.
Created Method for ASP.Net Web api
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