A root or starting point to the API is essential for an API to be HATEOAS-compliant. Nate demonstrates how to build a controller that returns the root document of the API.
- [Instructor] Let's build a simple root controller that can serve as the starting point of our API. First, I'll delete the demo controller that was added by the template. Then I'll create a new controller called root controller. By default, asp.net core nbc will look for any class with the suffix controller to discover the controllers in your project. We can inherit from the controller base class, which is in another name space, so we need to import that. API controllers use route attributes to tell the routing system which routes to handle.
In this case, the controller should respond to the route slash or root. If we create a get method, asp.net core nbc will automatically understand that we want this code to handle an incoming http get request. That's nifty, but we can also give the method a more descriptive name and mark it with an attribute. We can use http get attribute to say that this should handle an incoming get request. Returning I action result gives us the flexibility to return http status codes or JSON responses or both.
Eventually, our root controller will return links to all of the other routes available in the API. For now, let's keep it simple and just make it return a link to itself. I'll declare the result as an anonymous object with an href property. Then return ok, which will return http 200 and that JSON body. We want to return an absolute URL to the API, but ideally we shouldn't hard code the URL of the application into the controller code.
Fortunately, asp.net core nbc gives us an easy way to generate an absolute URL. We can use the URL dot link method and then pass a named route. In this case we'll use the name of the method itself as a convenient label. If there's any route parameters that need to be passed, they go in the second parameter, but in this case it's just null. We need to update the attribute on this method to name this route and again we'll just use the name of the method as a convenient label. This is nice because if we ever need to update the name of the method this will automatically follow.
Now asp.net core will generate a nice, absolute URL to the route and will return this inside of a JSON object. Next, let's test this controller.
- 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.