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.
- protected void RegisterRoutes
- {
- AreaRegistration.RegisterAllAreas();
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- BundleConfig.RegisterBundles(BundleTable.Bundles);
- }
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.

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.
- public class HomeController : Controller
- {
- [Route("contact")]
- public ActionResult Contact()
- {
- ViewBag.Message = "Your contact page.";
- return View();
- }
- }

Delpin Susai RajPosted Aug 28, 2016, 9:31 AM
Nice
Gowtham RajamanickamPosted Apr 7, 2016, 4:25 AM
Good Show.
Pradeep SahooPosted Apr 6, 2016, 11:47 PM
Thanks for sharing ...
Bhushan GawalePosted Apr 6, 2016, 10:21 PM
Good, thanks for sharing!
Mohammed IbrahimPosted Apr 6, 2016, 12:18 PM
nice
Nikhil RajputPosted Apr 6, 2016, 4:28 AM
Really Helpful
Rahul Kumar SaxenaPosted Apr 6, 2016, 2:11 AM
Good One
Mayank SharmaPosted Apr 6, 2016, 1:43 AM
Well done Mr. Mannan , thanks for sharing your knowledge.
Jaipal ReddyPosted Apr 5, 2016, 11:56 PM
Nice. .
Humayun Kabir MamunPosted Apr 5, 2016, 11:41 PM
Nice...
Hari ShankerPosted Apr 5, 2016, 11:33 PM
nice thanks for sharing
Ankur MistryPosted Apr 5, 2016, 2:41 PM
Good job. Thanks for sharing