In this video, Nate explains how to reuse the collection classes to return a collection of users in the system.
- [Instructor] First, let's create a user resource. This will go in the model's folder and we'll call this, user. This will be a resource and it'll have similar properties to the user entity. We'll have an email, a first and last name, and a date time offset that represents the created timestamp. Because it inherits from resource, it'll have an href as well. I've added a user service and a users controller to this project.
You can find this classes in the exercise files through this video. In the users controller, we'll add an implementation for the get visible users async method. Later, we'll add an authorization check here so that only administrators can see the whole collection of users in the system. For now, we'll return the entire collection. To get all the users, we'll call the user service, get users async passing the paging options, the sort options, the search options, and the cancellation token.
And then, we'll just build up a page collection. We'll create a link to this collection by naming this method get visible users async. And we'll simply return this collection back to the clan. Let's go over to the implementation of the service class and implement the get users async method here as well.
This code will look very similar to the other services. We'll declare an iqueryable of user entity and get that from the user manager .users property. As we've done in the past, we'll apply the search options and the sort options to the query. We'll get account of the total number of users in the collection. And we'll get all the items as well. We'll apply any paging options that the client sent.
And we'll project this to our new user class called user using Automapper. Finally, we'll return a new page results of user with the items and the size. As a last step, we need to tell Automapper how to map from user entity to the user resource. So, we'll open up the mapping profile. Then, add a new map between user entity and user.
For this, we'll just need to define how to map the self link. For this, we'll say link.to. We'll give it the name of the users controller get user by id async method and pass the user ID. Alright, let's try hitting this new users route and see what we get back. Using Postman, I'll create a request to localhost slash users and we get back our eye on collection, this time of user objects, with all of the properties of user mapped over to the resource.
If getting the user collection gives you user resources, it would make sense that posting to the users collection would create a user. In the next video, we'll add some code that allows the user to register and create a new account by posting to this route.
- 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
Building Web APIs with ASP.NET Corewith Chris Woodruff1h 7m Intermediate
Deploying ASP.NET Core Applicationswith 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.