+91-90427 10472
         
Dot net training in Chennai -Maria Academy

Routing in ASP.NET MVC

03 Nov 2023

Routing is a basic element in ASP.NET MVC that governs how URLs are mapped to controller actions in your application. It specifies how incoming requests are handled, as well as which controller and action methods are used to generate the answer. Routing is required when creating clean, user-friendly URLs for your web application.

The following are the main elements and ideas around routing in ASP.NET MVC:

Route Table: The routing configuration in ASP.NET MVC is normally set up in the ‘RouteConfig’ class in your project’s ‘App_Start’ folder. A set of route definitions is stored in the ‘RouteConfig’ class. The ‘MapRoute’ method is used to add these route definitions to the ‘RouteTable.Routes’ collection.

Route Definitions: A route specification contains the URL pattern, the default settings, as well as the controller and action to be executed when a specific URL is matched. An example of a route definition is as follows:

routes.MapRoute(

    name: “Default”,

    url: “{controller}/{action}/{id}”,

    defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }

);

In this case, the route matches URLs like /Home/Index and delivers them to the controller and action methods defined.

Route Parameters: You can include placeholders like {controller}, {action}, and {id} in the URL pattern encased in curly brackets in the route definition. The arguments indicated by these placeholders are those that will be taken from the URL and provided to the relevant action method. As demonstrated in the last example, the route parameters may also have default values.

Route Constraints: To limit the values that can be entered for route parameters, you can apply restrictions. To guarantee that only numerical values match the route, you can, for example, stipulate that the id parameter must be an integer.

Route Handling: The routing system looks at the URL in an HTTP request and attempts to match it with one of the route definitions in the route table. In the event that a match is discovered, the relevant controller and action are called using the URL’s extracted arguments.

Attribute Routing: ASP.NET MVC allows attribute routing in addition to convention-based routing (as demonstrated in the RouteConfig example). With attribute routing, you may use attributes to define routing information directly on your controller activities. This gives individual actions greater routing flexibility and control.

As an example of attribute routing, consider the following:

[Route(“products/details/{id}”)]

public ActionResult Details(int id)

{

    // Action logic here

}

You can have detailed control over the URL structure for every action method with attribute routing.

In summary, routing is an essential component of ASP.NET MVC because it makes it possible to construct RESTful web applications, organize controllers and actions more effectively, and produce neat, SEO-friendly URLs. When routing is set up correctly, requests are routed to the right controller actions, which provide the user the right response.

Social tagging: