In this video, Nate will explain how route templates can be used in ASP.NET Core MVC to route to controllers without hardcoding the route names.
- [Narrator] Let's add another controller to the project to handle the slash rooms route. This route will let the client search for a room in the hotel. So, in the controllers folder, I'll add a new controller, called Rooms Controller. (sound of typing on keyboard) And just like the root controller, I need to inherit from the base controller class. And I'll use the same pattern of, having a named method. We'll call it Get Rooms. For now, I just want the method to throw an exception.
So I'll throw Not Implemented Exception. And I need to add the Http Get attribute and name the routes, so I'll say this name is just the name of the method. We also need the route attribute on the controller. On the root controller, this was just slash, but we want this controller to handle requests to slash rooms. Instead of hard coding this string slash rooms, we can use a template string. So I can say in brackets, controller. The controller template will automatically match the name of the controller, excluding the word controller.
So this string will match slash rooms. This is nice, because if we ever rename the controller, we don't have to go update a bunch of strings. For now, the get rooms route won't do anything but throw an exception. But we can go add it to the root controller. (mouse clicking) Over in the root controller file, I can use the URL.Link method again, to return an absolute URL to the get all rooms route. I'll return a new element here, called rooms, and make this an ion compatible link with an href property.
(sound of typing on a keyboard) This time we'll need to say we want to find the name of the RoomsController.GetRooms method. And we aren't passing any parameters to this URL. So lets start up the API and try to hit it with a client. (sound of typing on a keyboard) When I hit the root route, I'll see the href of the root route, or the self referential link, as well as a new object with a link to the rooms route. And we have the URL of the rooms route automatically generated for us.
If I make a request to the new slash rooms route, we get an error because I'm throwing an exception. If I let Visual Studio continue, ultimately ASP.net core will return a 500, or internal server error back to the client. In the next video, we'll learn how to improve this error handling by serializing exceptions as a JSON document.
- 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.