Learn about the built-in conventions for routing requests to ASP.NET Web API controllers.
- [Instructor] Web API uses a couple of different techniques for routing a request to the appropriate controller and action method. The first of these techniques that we're gonna look at is called the routing table. The routing table is a table of URL path patterns that asp.net looks through to match the requested URL. It goes through them as you've defined them from top to bottom until it finds one that matches, and then it uses that one. In theory, you could route all of the requests for your entire API application with just a single routing rule, but you'd need to be very consistent with your naming conventions.
And if that's not for you, don't worry, we'll look a little bit later on at how to customize routes individually. Looking at the startup class inside the exercise files for 01_01 under the start folder, I've opened the solution and then gone to the startup.cs file. Here you can see the startup class which is an Owin startup class. That means that this class will run automatically for us by the Owin framework when this assembly loads.
In other words, when the web application first starts up. One of the settings you'll see here being configured inside of the startup on line 30 is the default API route being created and being added to a route's list. In addition to its name, you'll notice that a route pattern is being defined. The route specified here starts with /api which will help differentiate these routes from any routes that we're trying to match with an MVC controller inside the same project.
Perhaps confusingly, web API and MVC are set up in very much the same way but these are actually completely separate routes. You'll note that the controller and ID parts of the route are both surrounded with curly braces. That means that these portions of the URL path will be captured into placeholder variables. Web API will place the word controller after this matched portion of the path, and then look for a controller class with that name in your project to run it.
For example, in a get request to the path /api/order, order would match to the controller called order controller, and because we did a get http request, web API would run a method in that controller class with get at the beginning of the method's name. Another built in placeholder name is action. And that stands for the name of the action method to use inside the controller class. But you'll notice that that's omitted in this route here.
That's because web API's default convention matches routes using just the controller name, and the http verb. If we wanted to, we could add action to the route and make that part of this routing rule. But, I'm going to leave the default convention in place for now. Finally, the ID portion of the URL is marked as optional in the default section of the route definition. This allow us to omit it from a request and still match this routing rule.
Now that you've seen how the default routing rule conventions work in web API to turn a request into an action method, the next thing we'll look at is how the parameters for those methods are mapped from the request.
- Convention-based routing
- Binding your code to an HTTP request
- Validating models
- Using attributes to route requests
- Customizing attribute routes
- Data serialization and model binding
- Error handling
- Using exception filters and exception loggers
- API documentation and testing
- Securing your API