What are the three segments of the default route, that is present in ASP .Net MVC
C#?


The ASP.Net MVC Routing module is responsible for mapping incoming browser requests to particular MVC controller actions. When the ASP.NET MVC application launches then the application registers one or more patterns with the framework's route table to tell the routing engine what to do with any requests that matches those patterns. When the routing engine receives a request at runtime, it matches that request's URL against the URL patterns registered with it and gives the response according to a pattern match.

ASP.NET introduced Routing to eliminate the needs of mapping each URL with a physical file. Routing enables us to define a URL pattern that maps to the request handler. System.Web.Routing is used by the MVC framework, but it's also used by ASP.NET Dynamic Data. The MVC framework leverages routing to direct a request to a controller. The Global.asax file is that part of our application, where we will define the route for our application.

Below is the route config of an MVC application −

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 }
      );
   }
}

Route Name

A route is a URL pattern that is mapped to a handler. A handler can be a controller in the MVC application that processes the request. A route name may be used as a specific reference to a given route.

Constraints

A set of constraints to apply against the URL pattern to more narrowly define the URL that it matches.

URL Pattern

A URL pattern can contain literal values and variable placeholders. The literals and placeholders are located in segments of the URL that are delimited by the slash (/) character.

When a request is made, the URL is parsed into segments and placeholders, and the variable values are provided to the request handler. This process is similar to the way the data in query strings is parsed and passed to the request handler. In both cases variable information is included in the URL and passed to the handler in the form of key-value pairs. For query strings both the keys and the values are in the URL. For routes, the keys are the placeholder names defined in the URL pattern, and only the values are in the URL.

Defaults

When we define a route, we can assign a default value for a parameter. The defaults is an object that contains default route values. The three segments of a default route contain the Controller, Action and Id.

In the above URL, the corresponding controller and action will be matched. If we are not sending the controller and action method in the URL and based on the default route the corresponding controller action method will be invoked.


Updated on: 24-Sep-2020

749 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements