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.
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.
- public class HomeController : Controller
- {
- [Route("contact")]
- public ActionResult Contact()
- {
- ViewBag.Message = "Your contact page.";
- return View();
- }
- }
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.
- [Route("contact")]
- [Route("home/contact")]
- public ActionResult Contact()
- {
- ViewBag.Message = "Your contact page.";
- return View();
- }
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
- public class StudentInfoController : Controller
- {
- [Route("studentinfo/details/{id}")]
- public ActionResult Details(int? id)
- {
- return View();
- }
- }
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
- [Route("home/{action}")]
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
-
- public ActionResult About()
- {
- ViewBag.Message = "Your application description page.";
-
- return View();
- }
- }
What happens when you already specify the route on action method and you have already specified the route on controller?
For example,
- [Route("home/{action}")]
- public class HomeController : Controller
- {
- [Route("about")]
- public ActionResult About()
- {
- ViewBag.Message = "Your application description page.";
-
- return View();
- }
- }
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: