As you know, Routing is the important building block in ASP.NET MVC application.
Apart from convention-based routing, MVC 5 supports attribute routing.
In this blog, we will see attribute routing in MVC 5.
What is Routing?
Routing is the way to map your action of a controller. For more details view this blog MVC routing in details.
MVC5 supports a new type of routing, this is called Attribute Routing.
Can we use convention-based routing and attribute routing in a single project?
Yes, we can use both types of routing in a single project.
Why Attribute Routing?
As we know, in ASP.NET MVC, there is a file RouteConfig.cs which contains all custom routes we define for our application. If you are working on a big project, sooner or later your RoutConfig file will grow and could become a challenge to manage.
Another issue, if we go back to Controller class and changes Action name, then we have to go and make changes in RouteConfig file manually.
Consider below controller and routing.
public ActionResult PostByDate(int year, int month) { return Content(string.Format(“This post is published in year {0} and month {1}”, year, month)); } |
If we rename Action name PostByDate, we have to make similar change in RouteConfig file.
routes.MapRoute( “PostYear”, “post/{year}/{month}”, new { Controller = “Test”, action = “PostByDate” }, new { year = @”2017|2018″, month = @”d{2}” }); |
How to enable custom route using attribute routing in MVC5?
In order to implement Attribute Routing, we must enable it in RouteConfgi file. See the highlighted line below.
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); routes.MapMvcAttributeRoutes(); routes.MapRoute( name: “Default”, url: “{controller}/{action}/{id}”, defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional } ); } |
routes.MapMvcAttributeRoutes() actually maps the attribute-defined routes for the application.
How to define custom route using attribute routing in MVC5?
Consider below MVC routing, defined in RouteConfig.cs file
routes.MapRoute( “PostYear”, “post/{year}/{month}”, new { Controller = “Test”, action = “PostByDate” }, new { year = @”d{4}”, month = @”d{2}” }); |
Now, you want to define the same routing using attribute routing.
The first step is to enable AttributeRouting as shown above.
Now define attribute rout like this –
[Route(“post/{year}/{month}”)] public ActionResult PostByDate(int year, int month) { return Content(string.Format(“This post is published in year {0} and month {1}”, year, month)); } |
Apply constraints in Attribute Routing
[Route(“post/{year}/{month:regex(\d{2})}”)] public ActionResult PostByDate(int year, int month) { return Content(string.Format(“This post is published in year {0} and month {1}”, year, month)); } |
Hope you understand the Attribute Routing, to get better understanding open Visual Studio and give it a try.
Next –
- RenderBody() vs RenderSection()
- What is ViewStart.cshtml file?
- What is Controller in MVC?
- Anti-forgery tokens in MVC
- Data Annotation in MVC
Happy Learning 🙂