Your SlideShare is downloading. ×
Web api crud operations
Web api crud operations
Web api crud operations
Web api crud operations
Web api crud operations
Web api crud operations
Web api crud operations
Web api crud operations
Web api crud operations
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Web api crud operations

1,389

Published on

Web api crud operations

Web api crud operations

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,389
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • http://msdn.microsoft.com/en-us/library/ff649690.aspxhttp://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
  • Notice that the method return type is now HttpResponseMessage. By returning an HttpResponseMessage instead of a Product, we can control the details of the HTTP response message, including the status code and the Location header.The CreateResponse method creates an HttpResponseMessage and automatically writes a serialized representation of the Product object into the body fo the response message.
  • The method name starts with "Put...", so Web API matches it to PUT requests. The method takes two parameters, the product ID and the updated product. The id parameter is taken from the URI path, and the product parameter is deserialized from the request body. By default, the ASP.NET Web API framework takes simple parameter types from the route and complex types from the request body. 
  • According to the HTTP specification, the DELETE method must be idempotent, meaning that several DELETE requests to the same URI must have the same effect as a single DELETE request. Therefore, the method should not return an error code if the product was already deleted.If a DELETE request succeeds, it can return status 200 (OK) with an entity-body that describes the status, or status 202 (Accepted) if the deletion is still pending, or status 204 (No Content) with no entity body. In this example, the method returns status 204.
  • Transcript

    • 1. Web API CRUDOperations Eyal Vardi CEO E4D Solutions LTD Microsoft MVP Visual C# blog: www.eVardi.com
    • 2. Expert Days 2012 © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 3. HTTP Methods  GET ( retrieves )  PUT ( updates )  POST ( creates )  DELETE© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 4. Repository Pattern  Use a repository to separate the logic that retrieves the data and maps it to the entity model from the business logic that acts on the model.© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 5. Getting a Resource (GET) public IEnumerable<Product> GetAllProducts() return repository.GetAll(); public Product GetProduct(int id) Product item = repository.Get(id); if (item == null) throw new HttpResponseException( new HttpResponseMessage(HttpStatusCode.NotFound)); return item;© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 6. Creating a Resource (POST) public HttpResponseMessage PostProduct(Product item) { item = repository.Add(item); var response = Request .CreateResponse<Product>(HttpStatusCode.Created, item); string uri = Url.Link("DefaultApi", new { id = item.Id }); response.Headers.Location = new Uri(uri); return response;© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 7. Updating a Resource (PUT) public void PutProduct(int id, Product contact) contact.Id = id; if (!repository.Update(contact)) throw new HttpResponseException( new HttpResponseMessage(HttpStatusCode.NotFound));© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 8. Deleting a Resource public HttpResponseMessage DeleteProduct( int id ) repository.Remove(id); return new HttpResponseMessage( HttpStatusCode.NoContent );© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
    • 9. Web API CRUD Operations© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il

    ×