Hello all lovely people out there, the 4th article in the series is here. Over here we will have a detailed look on Routing, how does MVC finds the correct action method, how to configure certain routes and more.
In layman terms, Routing is nothing but the matching of the URL to a handler, which listens to the URL and acts on it.
In the traditional web forms scenario, the handler is a physical file, e.g an .aspx file.
In our MVC scenario, the handler is a class file which is basically a controller.
In fact, the above is one of the biggest difference between a web forms application and a MVC application, requests in a web forms app are mapped to the physical files present in the deployed folder, where as requests in a MVC app are mapped to class files.
Routing is not specific to MVC, in fact Routing feature is core to the ASP.NET framework, we can have routing enabled in a web forms app, a WCF service and so on.
ASP.NET MVC has popularized the concept of Routing.
So what exactly is a Route?
As per MSDN, Route is a URL pattern which is mapped to a handler.
As discussed above, the handler in case of a Web Forms App is a physical file, where as in case of a MVC app, handler is a class file.
How do we define or create a Route?
Well, we define a route by creating a class an instance of the Route class, and passing in the desired URL pattern and a friendly route name.
Post that, we add this route to the Routes property of the RouteTable class, for the route to be active and working.
Typically in a MVC app, in the RouteConfig.cs file, we see the below code:
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 }
);
}
}
And in the Global.asax file, we see the below code:
protected void Application_Start(){
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
In the static RegisterRoutes method, a new route with a friendly name as "Default" has been created, which is then added to the RouteTable object in the Application_Start() event of the Global.asax file. This ensures that the route is up and ready when the application starts.
This default route is created by VS itself when we create a MVC project, and obviously we can add our own custom routes as per the requirement, by calling the MapRoute extension method on the RouteCollection class.
Routes in a Web Forms application can be added by calling in the MapPageRoute() method on the RouteCollection class.
Let's discuss more on the URL patterns.
URL patterns consist of literals and placeholders. Placeholders are just like variables.
In MVC the general URL pattern is {controller}/{action}/{parameter}.
We see that we can have multiple placeholders in the pattern, but they have to be separated by a literal value. In the above pattern "/" is the literal value.
Let us try to test the above scenario, I have removed the "/" literal separating the {controller}/{action}, as below:
Let's try to browse by hitting on the play icon, we get the below yellow screen, which clearly says that
A path segment cannot contain two consecutive parameters. They must be separated by a '/' or by a literal string.
Parameter name: routeUrl
In layman terms, Routing is nothing but the matching of the URL to a handler, which listens to the URL and acts on it.
In the traditional web forms scenario, the handler is a physical file, e.g an .aspx file.
In our MVC scenario, the handler is a class file which is basically a controller.
In fact, the above is one of the biggest difference between a web forms application and a MVC application, requests in a web forms app are mapped to the physical files present in the deployed folder, where as requests in a MVC app are mapped to class files.
Routing is not specific to MVC, in fact Routing feature is core to the ASP.NET framework, we can have routing enabled in a web forms app, a WCF service and so on.
ASP.NET MVC has popularized the concept of Routing.
So what exactly is a Route?
As per MSDN, Route is a URL pattern which is mapped to a handler.
As discussed above, the handler in case of a Web Forms App is a physical file, where as in case of a MVC app, handler is a class file.
How do we define or create a Route?
Well, we define a route by creating a class an instance of the Route class, and passing in the desired URL pattern and a friendly route name.
Post that, we add this route to the Routes property of the RouteTable class, for the route to be active and working.
Typically in a MVC app, in the RouteConfig.cs file, we see the below code:
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 }
);
}
}
And in the Global.asax file, we see the below code:
protected void Application_Start(){
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
In the static RegisterRoutes method, a new route with a friendly name as "Default" has been created, which is then added to the RouteTable object in the Application_Start() event of the Global.asax file. This ensures that the route is up and ready when the application starts.
This default route is created by VS itself when we create a MVC project, and obviously we can add our own custom routes as per the requirement, by calling the MapRoute extension method on the RouteCollection class.
Routes in a Web Forms application can be added by calling in the MapPageRoute() method on the RouteCollection class.
Let's discuss more on the URL patterns.
URL patterns consist of literals and placeholders. Placeholders are just like variables.
In MVC the general URL pattern is {controller}/{action}/{parameter}.
We see that we can have multiple placeholders in the pattern, but they have to be separated by a literal value. In the above pattern "/" is the literal value.
Let us try to test the above scenario, I have removed the "/" literal separating the {controller}/{action}, as below:
Let's try to browse by hitting on the play icon, we get the below yellow screen, which clearly says that
A path segment cannot contain two consecutive parameters. They must be separated by a '/' or by a literal string.
Parameter name: routeUrl
Let's have a look as to how a route is selected and what happens behind the scenes, how is a particular action method of a particular controller executed. The detailed summary is as below:
- When a request is received in MVC, it is first routed to the UrlRoutingModule object, this UrlRoutingModule object parses the incoming request and selects the first route which matches the incoming request.
- If the match is successful, the module retrieves the IRouteHandler object for the matched route.
- In MVC apps, the IRouteHandler object will be the instance of the MvcRouteHandler class.
- The MvcRouteHandler instance creates a MvcHandler object. This MvcHandler typically implements IHttpHandler interface and is a HTTP Handler.
- The MvcHandler determines which Controller to invoke, by looking at the {controller} placeholder of the URL and adding the suffix "Controller" to it. The {action} placeholder determines which action method to be executed.
The summary concludes this article, in the next one we will see as to how a View is selected and rendered, what rules and selection criterion are followed. Till then see ya and stay tuned.
No comments :
Post a Comment