In this video, Nate will describe how to create and annotate a basic controller in ASP.NET Core that follows RESTful best practices.
- [Instructor] Now that we have some data, let's create a new controller to return it. I'm going to create another new controller called Info Controller. And we'll use the same pattern that we did when we created the Rooms Controller. We'll add a route attribute. With a value of slash controller. And inherit from the controller base class. In the pervious video, we put a hotel info class into the service container. We can use constructor injection to inject that into our controller.
So I can receive a type of IOptions which needs to be imported from the options namespace, of hotel info which needs to be imported from the models namespace. We'll call this hotel info accessor. And then we want to create a private variable of type hotel info, and we'll call this hotelInfo. In the constructor, we can set the local variable to hotelInfoAccessor.Value which unwraps the IOptions instance and gives us that hotel info class.
Now we can create a route called get info. And we'll mark it as HttpGet with a name of GetInfo. Now we can return the data. We can say return okay hotel info. And then before that we can set the href cause that's not automatically set. The href property we can set with Url.Link pointing to the GetInfo route, this route itself, and no route parameters.
That takes care of the info route. Let's go to the route controller and add that to the route response. We'll add one more element here called info. We'll say new href equals Url.Link pointing to the InfoController.GetInfo method. And no route parameters here either. So let's build this and test it out with Postman. If we hit the root route. We'll see a new element called info, a new link, if we follow that link, we'll see the info route which has all the data populated from app settings .json plus a self-referential link to the info route.
Returning static values from configuration is nice. But most of the time we'll need to read and write from a database. We'll connect an API to a database next.
- 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 Core (2016)with 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.