Back to: ASP.NET Core Tutorials For Beginners and Professionals
Attribute Routing in ASP.NET Core MVC Application
In this article, I am going to discuss Attribute Routing in ASP.NET Core MVC Application with examples. Please read our previous article before proceeding to this article where we discussed the Conventional Based Custom Routing in ASP.NET Core MVC Application. As part of this article, we are going to discuss the following pointers.
- Need and Use of Attribute Routing in ASP.NET Core MVC Application.
- What is Attribute Routing in ASP.NET Core MVC?
- Attribute Routing with Parameters in ASP.NET Core MVC ApplicationĀ
- Attribute Routing with Optional Parameters in ASP.NET Core MVC Application
- Controller and Action Method Names in Attribute Routing.
- Attribute Routes at Controller Level
- How to ignore the Route Template placed at the Controller Level?
Need and Use of Attribute Routing in ASP.NET Core MVC Application
Before understanding the Attribute Routing in ASP.NET Core MVC Application, let us first do some changes to our application. First, modify the Configure() method of the Startup.cs file as shown below. If you notice in the code, here we are using theĀ UseMvc()Ā method without passing the default route template as a parameter.
public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseMvc(); } }
With the above changes in the Configure method, now our application does not have any configured routes to handle the request and response.
Now Modify the Home Controller as shown below.
public class HomeController : Controller { public string Index() { return "Index() Action Method of HomeController"; } }
Now, when you navigate to any of the following URLs, you will get the 404 errors. This is because at the moment we donāt have any configured route in our application to handle the request.
- http://localhost:52190
- http://localhost:52190/home
- http://localhost:52190/home/index
Let us see how to use Attribute Routing to map the incoming URLs to the Index action method of Home Controller.
Attribute Routing in ASP.NET Core MVC:
With the help of ASP.NET Core Attribute Routing, you can use theĀ RouteĀ attribute to define routes for your application. You can use theĀ RouteĀ attribute either at theĀ ControllerĀ level or at theĀ Controller Action Methods level. When you apply the Route attribute at the Controller level, then it is applicable for all the action methods of that controller.
Let us modify the Home Controller as shown below. Here we have applied the Route Attribute at the Action method.
using Microsoft.AspNetCore.Mvc; namespace FirstCoreMVCApplication.Controllers { public class HomeController : Controller { [Route("")] [Route("Home")] [Route("Home/Index")] public string Index() { return "Index() Action Method of HomeController"; } } }
If you notice, here we applied theĀ Route() attribute 3 times on theĀ Index()Ā action method of Home Controller. The point that you need to remember is, with each instance of theĀ Route attribute we specified a different route template. With the above three Route attribute, now we can access theĀ Index()Ā action method of theĀ HomeControllerĀ using the following 3 URLs.
- http://localhost:52190
- http://localhost:52190/home
- http://localhost:52190/home/indexĀ
Now run the application and navigate to the above three URLs and you will see the output as expected.
Attribute Routing with ParametersĀ in ASP.NET Core MVC Application:
As we already discussed, with conventional based routing, we can specify the route parameters as part of the route template. We can also do the same with attribute routing. That means we can also define Route Attribute with parameters. To understand this, modify the Home Controller as shown below.
using Microsoft.AspNetCore.Mvc; namespace FirstCoreMVCApplication.Controllers { public class HomeController : Controller { [Route("")] [Route("Home")] [Route("Home/Index")] public string Index() { return "Index() Action Method of HomeController"; } [Route("Home/Details/{id}")] public string Details(int id) { return "Details() Action Method of HomeController, ID Value = " + id; } } }
As you can see in the above code, the Details()Ā action method has theĀ idĀ parameter. Notice in the route template, we also specified the id parameter. So the URLĀ (/Home/Details/10)Ā will execute theĀ Details(int id)Ā action method and maps the valueĀ “10”Ā to theĀ “id”Ā parameter of theĀ Details action method. This is done by a process calledĀ Model binding which will discuss in our upcoming articles. Now, run the application and navigate to the following URL and you will see the output as expected.
http://localhost:52190/Home/Details/10Ā Ā
Attribute Routing with Optional Parameters in ASP.NET Core MVC Application:
Like conventional based routing, we can also make a parameter as optional in Attribute Routing. To make the Route parameter optional, simply add a question mark ā?ā at the end of the parameter.
In our example, at the moment, the Details(int id) action method of the HomeController is executed only if we pass the id parameter value. If we have not passed the id parameter value in the URL, then we will get 404. For example, at the moment if we navigate to the following URL we will get a 404 error.
http://localhost:52190/Home/Details
Let us modify the Route attribute of the Details action method as shown below to make the route parameter “id”Ā as optional by adding aĀ “?”Ā at the end.Ā
using Microsoft.AspNetCore.Mvc; namespace FirstCoreMVCApplication.Controllers { public class HomeController : Controller { [Route("")] [Route("Home")] [Route("Home/Index")] public string Index() { return "Index() Action Method of HomeController"; } [Route("Home/Details/{id?}")] public string Details(int id) { return "Details() Action Method of HomeController, ID Value = " + id; } } }
Now run the application and navigate to the following URL and you will see the output as expected instead of 404 error.
http://localhost:52190/Home/Details/
Controller and Action Method Names in Attribute Routing:
With attribute routing in ASP.NET Core MVC Application, the controller name and action method names do not play any role. To understand this, modify the Home Controller as shown below.
using Microsoft.AspNetCore.Mvc; namespace FirstCoreMVCApplication.Controllers { public class HomeController : Controller { [Route("")] [Route("MyHome")] [Route("MyHome/Index")] public string StartPage() { return "StartPage() Action Method of HomeController"; } } }
As you can see, we have specified the Route attribute three timesĀ StartPage() action method of theĀ HomeController. So, this StartPage action methodĀ is going to be executed for the following 3 URLs.Ā Ā
/
/MyHome
/MyHome/Index
Attribute Routes at Controller Level:
In the ASP.NET Core MVC application, it is also possible to apply the Route() attribute on the Controller class as well as on individual action methods. If you want to make the attribute routing less repetitive, then you need to use the route attributes on the controller level as well as on the individual action methods level.Ā
Let us understand this with an example. First, modify the Home Controller class as shown below.
using Microsoft.AspNetCore.Mvc; namespace FirstCoreMVCApplication.Controllers { public class HomeController : Controller { [Route("")] [Route("Home")] [Route("Home/Index")] public string Index() { return "Index() Action Method of HomeController"; } [Route("Home/Details/{id?}")] public string Details(int id) { return "Details() Action Method of HomeController, ID Value = " + id; } } }
With the above code in place, we can access the Index()Ā action method using the following 3 URLs.
/
/Home
/Home/Index
Along the same line, we can also access the Details(int? id) action method using the following 2 URLs.Ā
/Home/Details
/Home/Details/2Ā
If you notice, we have repeated the word Home multiple times (four times in our example). In order to make these routes less repetitive, we need to apply theĀ Route()Ā attribute with the word Home at theĀ HomeControllerĀ class level as shown below.Ā
using Microsoft.AspNetCore.Mvc; namespace FirstCoreMVCApplication.Controllers { [Route("Home")] public class HomeController : Controller { [Route("")] [Route("Index")] public string Index() { return "Index() Action Method of HomeController"; } [Route("Details/{id?}")] public string Details(int id) { return "Details() Action Method of HomeController, ID Value = " + id; } } }
Note: The Route template applied on the controller level is prepended to the route template applied to the action method level.
Now when you navigate to the following four URLs you will get the output as expected.
However, when you navigate to the root URL (http://localhost:52190) of the application, you will get a 404 error. In order to solve this, you need to include the route template that begins with “/”Ā on theĀ Index()Ā action method as shown below.Ā
With the above changes in place, now run the application and navigate to the root URL and you will see the output as expected.
How to ignore the Route Template placed at the Controller Level?
In order to ignore the Route Template placed at the Controller level, you need to use / or ~/ at the action method level. If the action method route template starts with / or ~/, then the controller route template is not going to be combined with the action method route template.
To understand this let us modify the Home Controller class as shown below. In the following code, the About action method starts with ~/, so this action method is not going to be combined with the controller route template.
Now run the application and navigate to /About URL and you will see the output as expected.
Thatās it for today. In the next article, I am going to discuss Tokens in Attribute Routing in ASP.NET Core MVC Application. Here, in this article, I try to explain Attribute Routing in ASP.NET Core MVC Application. I hope you enjoy this article.
nice. this let me understand the concept of global template / detail template. thank you
nice