Attribute routing is used in the controllers to describe the routes the ASP.NET MVC middleware will follow for each action. This method is more efficient for use with Web API development. In this course, we show how to decorate your controllers with attri
- [Instructor] We looked at the template routing method in the previous video. Now we're going to look at routing for the API developer. This routing method is called attribute routing. Attribute routing is very easy to work with. Usually, I think it's easier than template routing. I created a small demo app that we will use for this video. Let's open up the ASP.NET Core solution for this video. The project is named AttributeRouting.
First, let's take a look at the Startup.cs file. In the configured method, we have the line app.UseMvc. In a previous video, we showed that the UseMvc method can take a lambda for the routes that will be used in the project. For attribute routing, we won't be using template routes. Instead, we'll be using attributes on the controllers. So you don't to pass anything to the UseMvc method. If route templates are not passed in the UseMvc method, then attribute routing will be used throughout the routing middleware.
Open up the ValuesController in the Controller folder. We have our value controller class with a number of methods. To get the routing middleware to know that this is the value controller, we need to add an attribute to the value controller class. Add the attribute Route above the class. You'll notice you get an error. That's because we need to also give the details about the route. There are two ways that we can declarate the route.
We can have an explicit reference to the model it corresponds to. Let's add that declaration now. The error goes away. Instead, we can just add [Controller]. That will take the name of the model referenced in the controller file name. I would suggest using this form of routing attribute for the controller to leverage the conventions of ASP.NET Core MVC.
Our Web APIs get called with two inputs. First is the URL and the second is the HTTP verb. I know there's other things involved, like the HTTP header, but let's just focus on the URL and the verb. The URL with the HTTP verb will allow us to determine the route needed for the request. What we need to do now is to allow the routing middleware to know what action will be called for a given HTTP verb and parameter set.
We need to add a route action HTTP attribute to each controller method so the middleware can know how to direct the action, depending on the verb. Let's take a look at the available route action HTTP attributes for developers to use. There's [HttpGet], [HttpPost], [HttpPut], and [HttpDelete]. These four will be used the most often.
There are a few others, but we will not be looking at those during this course. Let's stay in the CustomerController.cs file. There are two methods, or actions, to add attributes to. The Get() method will get all of our values. Let's add [HttpGet] to this action so that the routing middleware knows that when we receive a Get() verb for the values route, this action will be called.
The other Get() method takes the ID parameter that will send back a value based on the ID. This also uses the [HttpGet] attribute but adds an ID parameter so that the middleware knows to pass the ID value sent from the URL. Let's add our [HttpGet] to this method. Now we know when a Get() verb is requested with a parameter, it will be directed to the action. Let's run the app and see what we get.
You see that we get all four fruit objects coming back when we call /api/values. Now let's just get one of the objects by passing an ID. If I send four back, I should get the peach object, which I do. We can see that attribute routing is simple to use, and we will be utilizing this method throughout the course.
- Installing Visual Studio and SQL Server
- Creating your Web API project
- Routing in ASP.NET Core
- Adding and connecting controllers
- Modeling binding and validation
- Measuring performance