Learn how to create an action method that handles the DELETE verb, and delegates to the service layer to delete the resource from the database.
- [Instructor] Now that we've given the client a way to create resources using POST, we should also allow the opposite and implement the delete verb. When and where to allow the client to delete a resource, depends on the domain of your API. In the land and hotel API, it makes sense to let a user delete bookings that they created. We can implement this in the booking controller. We can create another route on the controller called Delete Booking By ID Async. This route will just take a Guid booking ID, and the cancellation token.
We'll mark this with an HTTP delete attribute, and include in the route template the booking ID parameter. And as with the other routes we'll name this after the method itself. Inside of this method we'll simply say await booking service.delete booking async, which is a method we'll need to implement. Passing the booking ID and the cancellation token. And regardless of the outcome of that operation, we'll return back to the client no content. It's important that we add authorization here later to make sure the user is actually allowed to delete this booking.
Let's go over to the booking service interface, and we'll add this method. This will simply return a task, it'll be called Delete Booking Async, and it'll take the booking ID, and a cancellation token. Now we need to add it to the service implementation. We can use Visual Studio to implement the method stub for us, and for this method we simply need to pull the booking out of the context, by calling single or default Async.
And searching for the booking ID, we need to add the Async keyword. If the booking is null or the ID is invalid, we'll simply return. And if it does exist, we'll say context.bookings.remove, give it that entity, and then make sure we save those changes back to the context. Let's give it a try. Since I've restarted the application since I sent these earlier requests, this booking doesn't exist anymore. If I send a request here I'm just going to get 404.
What I can do is recreate the booking, so send another POST request to room/bookings. Which will create a new booking instance, and I'll retrieve that. So here's our created booking in the database. Now I can send a delete request to remove it from the database. Then I'll get 204 no content back. If I try to retrieve it again, it'll be gone, 404. As you can see using the HTTP attributes in ASP.NET Core to add additional verbs like delete is pretty straightforward.
- REST vs. RPC
- Using HTTP methods (aka verbs)
- Returning JSON
- Creating a new API project
- Building a root controller
- Routing to controllers with templates
- Requiring HTTPS for security
- Creating resources and data models
- Returning data and resources from a controller
- Representing links (HREFs)
- Representing collections
- Sorting and searching collections
- Creating forms
- Caching and compression
- Authentication and authorization for RESTful APIs
Skill Level Intermediate
Deploying ASP.NET Core Applications (2017)with Nate Barbettini57m 57s Intermediate
1. REST API Concepts
2. Building a Basic API
3. Securing the API
4. Representing Resources
5. Representing Links
6. Representing Collections
7. Sorting Collections
8. Searching Collections
9. Forms and Modifying Data
10. Caching and Compression
11. Authentication and Authorization
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.