Introduction
Here I will explain how many types of routing there are in MVC and how it works in the project.
What is Routing?
Routing is a pattern matching system. Routing maps an incoming request (from the browser) to particular resources (controller & action method). This means routing provides the functionality to define a URL pattern that will handle the request. That is how the application matches a URI to an action.
URL Pattern
- domain.com/Home/about/1
domain.com is a namespace or hosting name
Home is a Controller name
about is an action method name
1 is id
General process of request: Client sends the request to the server and processes it and produces the output.
How To Work Routing
Routing parses the request in route configuration. Then, it matches the request in the route table to ensure which controller and which action will be processed.
Route
Basically, Route is an approach which defines URL pattern and handles the information. In MVC, routes decide which Controller method to be executed on a particular request.
An MVC URL consists of the following properties.
- Route Name: A route is a URL pattern that is mapped to a handler.
- A handler has a controller that can process the request.
Configure a Route
Each MVC application has a default route which is defined in RouteConfig class under the App_Start folder. The following image describes the configuration of a Route in the RouteConfig class.
In the above image, you see the Maproute(). it is a method of RouteCollection class used to configure a route in our application. It contains the name "Default". The URL pattern is "{controller}/{action}/{id}" and default parameter is for controller, action method, and id parameter.
Route Table
- We define a route for each action method.
- All the routes are stored in the route table.
- Each incoming request is mapped to this route table.
- If a URL match is found then request goes to the related controller action method.
- If the URL is not found, the application returns a 404 page.
Types of Routing
There are 2 types of Routing in MVC application
- Conventional or Traditional Routing (Using Routing Config)
- Attribute Routing (Available in MVC 5)
Conventional or Traditional Routing
- Conventional or Traditional Routing also is a pattern matching system for URL that maps incoming request to the particular controller and action method.
- We set all the routes in the RouteConfig file.
- RouteConfig file is available in the App_Start folder.
- We need to register all the routes to make them operational.
Route
Route is a pattern for a particular URL. It needs the following parameters.
- Name
- Pattern
- Default values
- Constraints (if any)
Example
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
RouteConfig file
- public class RouteConfig
- {
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
-
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
- }
- }
Register RouteConfig in Global.asax
- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- }
Attribute Routing
It is a very simple routing method compared to conventional routing. All the concepts are just like the conventional approach but here, we define all the routes and attributes. In attribute, we define the routing on a simple controller or action method.
How to register attribute routing
For simply registering the attribute routing in our application, we need to add the following line in routeconfig.cs.
- routes.MapMvcAttributeRoutes();
How to use attribute routing
Simply, we can use Routeprefix or route attribute for using attribute routing in the controller.
- [RoutePrefix("Home")]
- public class HomeController : Controller
- {
- [Route("Index")]
- public ActionResult Index()
- {
- return View();
- }
- }
Summary
In this article, I have discussed MVC routing. I hope you will find it useful.