In this video, learn how to create and annotate a basic controller in ASP.NET Core that follows RESTful best practices.
- [Instructor] Let's create a new controller to return this data. Create a new class in the Controllers folder. And we'll call this one InfoController. I'll use the same routing pattern as I did when I created the RoomsController. This needs to inherit from ControllerBase. And here we'll say Route /[Controller], we'll make this an ApiController.
Then we'll create a method. This method will return action result of hotel info which is the model we'll return. We'll call this GetInfo. And we'll mark it with HttpGet. Say the name is nameof(GetInfo). We'll say that it ProducesResponseType(200). For now we'll just throw an exception.
Returning action result of HotelInfo instead of just IActionResult tells the ASP.net core that we'll be returning a strongly typed model from this method. Before we return anything, let's inject the IOptions of hotel info at the top here. We'll say we have a private variable called _Hotelinfo and a constructor which takes an Ioptions of HotelInfo. We'll need to import this namespace. Ioptions of HotelInfo.
Ioptions is just a wrapper around of any data that you push into the service container. So we'll say, this is called hotelInfoWrapper, and when we get the value in here, we'll do hotelInfoWrapper.value. Now in this method we'll set the Href property of hotelInfo which isn't populated automatically we'll use URL.Link for that, and say nameof(GetInfo) with no route parameters.
And then we can simply return_hotelInfo. Before we try this out, let's go over to the route controller and add this as a link underneath rooms, we'll say info equals new. Href equals Url.Link, nameof(infocontroller.GetInfo) no parameters. Alright, let's try this out in Postman and see what happens.
So hitting the root route, we'll get rooms and info. Let's try going to info, and we've been able to pull out that data from the configuration. Returning these static values from configuration is nice, but most of the time we'll need to read and wite to a database. We'll connect our ABI to a database next.
- What is RESTful design?
- Building a new API with ASP.NET Core
- Using HTTP methods
- Returning JSON
- Creating RESTful routing with templates
- Securing RESTful APIs with HTTPS
- Representing resources
- Representing links
- Representing collections
- Sorting and searching collections
- Building forms
- Adding caching to an ASP.NET Core API
- Configuring user authentication and authorization
Skill Level Intermediate
1. REST API Concepts
2. Build a Basic API
3. Versioning and Errors
4. Secure the API
5. Represent Resources
6. Represent Links
7. Represent Collections
Add pagination7m 37s
8. Sorting Collections
9. Searching Collections
10. Forms and Modifying Data
11. Caching and Compression
12. 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.