Route Attribute In ASP.NET MVC

Generally the application is developed in classic ASP and ASP.NET, and URLs are directly mapped to the physical file on the web server. For example, when you hit the following url in browser, you can be notified that the C-sharpcorner.com domain has the directory named forums under the root structure that contain the AddPost.aspx page.

http://www.c-sharpcorner.com/forums/AddPost.aspx

Here URL is directly mapped to the physical file on disk.

But this is not the case with ASP.NET MVC, because in MVC web framework URL is mapped directly to the controller action method instead of the physical file location.

Routing is a technique of creating a custom URL pattern that either maps to the controller action method or any physical file of the server.

Now you have a basic idea about the routing, so let’s start with a practical demo.

When you create a new ASP.NET project you will find the following code block in global.asax.cs in Application_start() event.

  1. protected void RegisterRoutes  
  2. {  
  3.       AreaRegistration.RegisterAllAreas();  
  4.       FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
  5.       RouteConfig.RegisterRoutes(RouteTable.Routes);  
  6.       BundleConfig.RegisterBundles(BundleTable.Bundles);  
  7. }  

In the above code there is a method call to RegisterRoutes(). This method's definition is available in RouteConfig.cs file inside the App_start folder.

When you start your mvc project, application_start() event calls the RegisterRoutes() method and then after RegisterRoutes() method it will create the route table. This route table contains the entry for the route.by default there is a single entry for route named “Default”. Before using Route attribute you have to enable it by calling the routes. MapMvcAttributeRoutes(); inside the RegisterRoutes() method.

Inthe above code there is a method call to RegisterRoutes(). This method's definition is available in RouteConfig.cs file inside the App_start folder.

When you start your mvc project, application_start() event calls the RegisterRoutes() method and then after RegisterRoutes() method will create the route table. This route table contains the entry for the route. By default there is a single entry for route named “Default”. Before using Route attribute you have to enable it by calling the routes. MapMvcAttributeRoutes(); inside the RegisterRoutes() method.

 
This route table task is to map the incoming request URL with the controller action method.

Action Routes

How to define static route attribute on controller action method

Just write the Route attribute on action method with template name. Template name is nothing but a string you passed in Route i.e. “contact”. This template name diagnoses the incoming request URL and decides whether to call the action method or not based on URL match criteria.

  1. public class HomeController : Controller  
  2. {  
  3.         [Route("contact")]  
  4.         public ActionResult Contact()  
  5.         {  
  6.             ViewBag.Message = "Your contact page.";  
  7.             return View();  
  8.         }  
  9.  

When the request comes with the /contact URL the route attribute will call the Contact action method.

You can also write multiple routes onthe same action or controller. For example if you want to call Contact action method with /Home/contact url just write the [Route("home/contact")] on action method.

  1.  [Route("contact")]  
  2.  [Route("home/contact")]   
  3.  public ActionResult Contact()  
  4.  {  
  5.             ViewBag.Message = "Your contact page.";  
  6.             return View();  
  7.  }   

How to define dynamic route attributes on action method.

Dynamic route attributes are useful when url contains the dynamic value. For example, to display particular student records at that time your url might look like,

/StudnetInfo/Details/1

  1. public class StudentInfoController : Controller  
  2. {  
  3.         [Route("studentinfo/details/{id}")]  
  4.         public ActionResult Details(int? id)  
  5.         {  
  6.             return View();  
  7.         }  
  8. }    

Now when the request comes with /studentinfo/details/1 url. {id} segment will be replaced with the value that is after /studentinfo/details/ the and final url looks like the studentinfo/details/1.

Here route attributes decompose the incoming request url and place the values in RouteValueDictionary as in a key value form.

Controller Routes

Generally all the action methods inside the specific controller follows the same url pattern except the action name.

For example all the action methods in Home controller follows the same url pattern,

  • Home/Index
  • Home/About
  • Home/Contact

So it’s better to avoid repeating the same route attribute on multiple actions.i.e

  1. [Route("home/{action}")]  
  2. public class HomeController : Controller  
  3. {  
  4.         public ActionResult Index()  
  5.         {  
  6.             return View();  
  7.         }  
  8.   
  9.         public ActionResult About()  
  10.         {  
  11.             ViewBag.Message = "Your application description page.";  
  12.   
  13.             return View();  
  14.         }  
  15. }    

What happens when you already specify the route on action method and you have already specified the route on controller?

For example,

  1. [Route("home/{action}")]  
  2. public class HomeController : Controller  
  3. {  
  4.        [Route("about")]  
  5.        public ActionResult About()  
  6.        {  
  7.            ViewBag.Message = "Your application description page.";  
  8.   
  9.            return View();  
  10.        } 
  11. } 

In the above case the controller level route will be overridden by the action route.

I hope you have enjoyed this article. If you have any questions or feedback, then please mention it in the comments section that will help me to improve my knowledge.
 
Read more articles on ASP.NET:


Similar Articles