A root or starting point to the API is essential for an API to be HATEOAS compliant. Learn how to build a controller that returns the root document of the API.
- [Instructor] Let's build a simple route controller that will serve as the starting point for the API. First, I'll delete this default values controller that was created by the template. And then, in the Controllers folder, I'll add a new class called Root Controller. Controllers in ASP.Net Core inherit from the Controller class. Lemme import the name space here, aspnetcore.nbc. For API controllers, you can use the controller base class, which is a stripped-down version without the view and razor features you only need in a web app.
API controllers use route attributes to tell the routing system which routes they should handle. In this case, this controller should handle the slash, or root route. So we'll say slash. I'll also add the API controller attribute. This lets ASP.Net Core know that I'm building a controller meant for an API. This opts us in to some nice extra features like automatic model validation. If we create a method called Get, ASP.Net Core MVC will automatically understand that we want this code to handle an incoming http Get request.
That's a nifty convention, but we can also give the method a more descriptive name like GetRoot and mark it with the http Get attribute. This explicitly tells ASP.Net Core that it should handle the Get verb. Returning iActionResult gives us the flexibility to return http status codes or JSON responses. Eventually, this root controller will return links to all of the other routes and controllers available in the API. For now we'll just keep it simple and return a link to self. I'll do that by saying var response equals just make a new anonymous object, href equals, need to put the absolute href to this control there somehow.
Then I'll do return OK, 200 OK with this response in JSON. I wanna return the absolute url to this API, but ideally I shouldn't be hard-coding things like absolute urls into my controller code. Fortunately, ASP.Net Core gives us an easy way to generate that absolute url. I can say url.link and then I need to pass a route name and route parameters. For the route name, I'll just use name of this method, GetRoot, and in this case, there are no route parameters.
So I'll just pass null. Now I need to update this method attribute to say name equals, and explicitly give it the name of the method. Now ASP.Net Core will generate a nice absolute url to this route, and then will return the whole thing as a JSON object. Next we'll go ahead and test this controller.
- 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.