Today we will have a look at one of the new features introduced in ASP.NET MVC 5, attribute-based routing.
Pre-context
We all know that ASP.NET MVC is a great platform that allows us to create and manage web applications in a much simpler manner compared to form-based web applications. There are a few things in MVC-based web applications that work a little differently than standard web applications, one of them is routing.
Until now, there has been a routing table that you can define either in the Global.asax or in the RouteConfig.cs and all incoming requests would look it up to decide the rendering of a target view.
Here is the code that you might have seen previously to have note-variable routes in MVC 4 in the following example of the Route collection object.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Product", action = "List", id = UrlParameter.Optional }
);
The big question
What is the need for this new routing methodology?
And the answer is that there was nothing wrong with the previous approach of routing and in fact, you can still use it in MVC 5 or use this new routing method in conjunction with the old one.
A few advantages of attribute-based routing
Here are a few advantages of attribute-based routing,
- Helps developer in the debugging/troubleshooting mode by providing information about routes.
- Reduces the chances for errors, if a route is modified incorrectly in RouteConfig.cs then it may affect the entire application's routing.
- May decouple controller and action names from route entirely.
- Easy to map two routes pointing to the same action.
All right, enough of talking. Let's see exactly how we can configure it and see if it works.
First, we will need to enable attribute-based routing on our MVC web application that can be done by only one line. All you need to do is put this line in the RegisterRoutes Method of the application.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
tes.MapMvcAttributeRoutes(); //Enables Attribute Based Routing
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Product", action = "List", id = UrlParameter.Optional }
);
}
Now, here is how you can use attribute-based routing on a specific action method.

Saad KhanPosted Sep 23, 2021, 7:47 AM
In 2nd code snippet, kindly correct line number 5 as routes.MapMvcAttributeRoutes.
CHANDRAN KANDANPosted May 18, 2020, 10:23 PM
Hi, when I using the routeprefix, I can't set default route
Farhan AhmedPosted Jul 30, 2018, 6:33 AM
Nice article.........
Dr.Ajay KashyapPosted Sep 14, 2017, 8:59 AM
Sir how to hide id from url
saurav singhPosted Aug 2, 2017, 2:12 AM
How can i achieve it domain.com/slug
hardik gheewalaPosted Jul 23, 2017, 2:00 PM
Hello Nice article. How to pass object with Http post method ?
Anil Kumar MurmuPosted Dec 15, 2016, 7:49 AM
Thank you for sharing the article.
Akhilesh PanditPosted Jun 3, 2016, 5:51 AM
Very helpful article thanks for this...
Bhushan GawalePosted Apr 21, 2016, 12:43 AM
Thanks everyone!
Kuppurasu NagarajPosted Apr 8, 2016, 2:27 PM
Nice
Vignesh ManiPosted Feb 27, 2016, 5:39 PM
Nice one
Mukesh KumarPosted Nov 20, 2015, 5:55 AM
Good Job
Pankaj BajajPosted Jan 7, 2015, 8:08 AM
new things learned today. Thanks Bhushan for sharing.
Abhijit KakadePosted Jan 7, 2015, 2:45 AM
good one..
Manish Kumar ChoudharyPosted Jan 6, 2015, 12:01 PM
Nice one... Thanks for sharing. ..