Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

ASP.NET Web API, Part 3: Delete and Update

0 (0 Likes / 0 Dislikes)
  • Embed Video

  • Embed normal player Copy to Clipboard
  • Embed a smaller player Copy to Clipboard
  • Advanced Embedding Options
  • Embed Video With Transcription

  • Embed with transcription beside video Copy to Clipboard
  • Embed with transcription below video Copy to Clipboard
  • Embed transcript

  • Embed transcript in:
    Copy to Clipboard
  • Invite a user to Dotsub
[Microsoft ASP.NET] Hi, this is John Galloway from Microsoft. We're continuing our look at ASP.NET Web API. In the last screen cast, we looked at retrieving information from the server using an HTTP get request. In this screen cast, we're going to take that further by posting new comments back to the server and deleting existing comments. So you'll see when we're done, we'll be able to click on these delete links and that's going to be making an AJAX call back to the server and deleting those comments. Similarly, we'll be able to fill out this form, click on the Submit button, and that's going to make an AJAX call back to our service end point, which is going to create the comment and return the information back to us for display. So let's take a look at how this works back on the server. We're continuing to work in our comments controller, which builds off an API controller-base class, and since delete my ID is very similar to get my ID, we're going to just copy an update that method. So the first step is renaming to delete and that's going to automatically by convention map to the delete HTTP verb. So the only real difference here is that when we've retrieved our comment, then we need to delete that. We'll just call into our repository's delete my ID method. And again, as we did with get, if we didn't find the comment by ID, we need to return that HTTP status code not found. So now let's move onto the create example, and we're taking advantage of 2 things in ASP.NET Web API. First is model minding. We're able to just accept a comment as a parameter and that's automatically going to be bound from form fields. Secondly, we're making use of the HTTP response message, and that is generically typed, and so we're typing this as a type of comment. That allows us to do 2 things. One, return content back to the client in the appropriate format, Json, XML, etc., but also to include the right headers along with that, which is very important for HTTP-centric APIs. So first we're going to use our repository's add method to add that comment to the repository and we're retrieving it back so that we can get access to the ID. Next, we need to add the right headers to that response, so we're using the HTTP response message of type comment, and we are returning the comment, but we are also returning the right status code, and in this case, we've created a new entity, so that's HTTP status code 102 created. So this is an important feature of ASP.NET Web API is it makes it very easy to return the right HTTP status codes. Do the right thing by default. So now, we also, since we've created a new entity, it's important to return the location of that new entity. So we're creating a response header pointing to that location. In this case, we're just building up that URI by hand. We're just contactinating strings. We could also have made use of the routing features that are built into ASP.NET Web API. So now that we've built up the correct response, we can just return it. Let's take a look at the JavaScript that's going to process this on the client. So in the post-case, first we're packaging up the information from the form into Json data to submit back to the server. Then we're sending that to the correct URL of type post, so that's going to create new information. And if the server returns an HTTP 201—remember that's created— then we'll know that our request to create this new comment has succeeded. Now for the delete case, first we get the ID, then we're going to call back into the correct URL with the delete HTTP verb, and if we receive an HTTP 201 from the server, that means that our request has completed satisfactorily and that comment has been deleted. Then we know to update our display and remove that comment. Now 1 good reason that our request to delete a comment by ID might fail is if that request ID is not found, and you'll see that our delete by ID is returning that status code, so we could handle that on our client if we wanted. So now let's take a look at this in action. We'll look at the IE developer tools by using F12, we're going to start monitoring our network traffic, and when we click on these delete requests, we'll see that we're using an HTTP delete verb and we're getting back an HTTP 200 OK message or response message after deleting them. So now we'll create a new comment. We'll see when we fill in the form that the values are posted back via Json, we get an HTTP 201 response code, which means that the content was created, and we also get a link back to the location of that newly created entity. So that wraps up our look at creating and deleting content using ASP.NET Web API. In the next screen cast, we'll look at advanced querying, including paging and OData support. [Microsoft ASP.NET]

Video Details

Duration: 5 minutes and 38 seconds
Country: United States
Language: English
License: All rights reserved
Genre: None
Views: 7
Posted by: neudesicasp on Jul 12, 2013

Note: This video contains information from a pre-release version of ASP.NET Web API. For information on what changed, see the updated sample code at http://code.msdn.microsoft.com/ASPNET-Web-API-JavaScript-d0d64dd7

Caption and Translate

    Sign In/Register for Dotsub to translate this video.