Routing in ASP.NET MVC: A Beginner's Guide

Category > ASP.NET MVC || Published on : Friday, March 17, 2023 || Views: 1176 || ASP.NET MVC Routing C# Code URL Mapping Custom Routes


ASP.NET MVC provides various features to make web development easier and faster, and one of the important features is routing. Routing in ASP.NET MVC is the process of mapping a URL to a specific controller and action method. In this article, we will discuss how routing works in ASP.NET MVC, and how to define custom routes using C# code.

Introduction:

ASP.NET MVC is a popular framework for developing web applications, and it provides various features to make web development easier and faster. One of the important features of ASP.NET MVC is routing, which helps to map incoming URLs to specific actions or controllers in the application. In this article, we will discuss routing in ASP.NET MVC, and how it works with the help of an example.

What is Routing in ASP.NET MVC?

Routing in ASP.NET MVC is the process of mapping a URL to a specific controller and action method. When a user requests a page or resource from the web server, the server uses routing to determine which controller and action method to invoke to handle the request. Routing is an important aspect of ASP.NET MVC, as it enables developers to create clean and SEO-friendly URLs, and it also makes it easier to manage and maintain the application.

How Routing Works in ASP.NET MVC?

Routing in ASP.NET MVC works by defining a set of rules that map incoming URLs to specific controllers and actions. These rules are defined in the RouteConfig class, which is located in the App_Start folder of the application. The RouteConfig class contains a RegisterRoutes method, which is responsible for defining the routing rules for the application.

The routing rules are defined using the MapRoute method, which takes three parameters: name, URL pattern, and defaults. The name parameter is a unique identifier for the route, and the URL pattern is a string that defines the format of the URL that the route will match. The defaults parameter is a set of default values that will be used if the route parameters are not specified in the URL.

Here is an example of a routing rule that maps a URL to a controller and action method:

routes.MapRoute(
    name: "Product",
    url: "product/{id}",
    defaults: new { controller = "Product", action = "Details", id = UrlParameter.Optional }
);

In this example, the name of the route is "Product", and the URL pattern is "product/{id}". The {id} placeholder in the URL pattern will match any value that is specified in the URL, and it will be passed as a parameter to the action method. The defaults parameter specifies that if the {id} parameter is not specified in the URL, the action method "Details" of the "Product" controller will be invoked.

Defining Custom Routes in ASP.NET MVC:

In addition to the default routing rules provided by ASP.NET MVC, developers can also define custom routing rules to handle more complex scenarios. Custom routes can be defined using the MapRoute method in the RouteConfig class, and they can be used to handle URLs that do not match the default routing rules.

Here is an example of a custom routing rule that maps a URL with two parameters to a controller and action method:

routes.MapRoute(
    name: "Search",
    url: "search/{term}/{page}",
    defaults: new { controller = "Search", action = "Index", term = UrlParameter.Optional, page = UrlParameter.Optional }
);

In this example, the name of the route is "Search", and the URL pattern is "search/{term}/{page}". The {term} and {page} placeholders in the URL pattern will match any values that are specified in the URL, and they will be passed as parameters to the action method. The defaults parameter specifies that if the {term} and {page} parameters are not specified in the URL, the action method "Index" of the "Search" controller will be invoked.

Conclusion:

Routing is an important feature of ASP.NET MVC, and it helps to map incoming URLs to specific controllers and actions in the application. In this article, we discussed how routing works in ASP.NET MVC