---
title: "How does routing work in ASP.NET MVC and how is it configured?"  
description: "How does routing work in ASP.NET MVC and how is it configured?"  
author: "Revati S Misra"  
published: 2023-06-01  
updated: 2023-06-04  
canonical: https://www.mindstick.com/forum/158586/how-does-routing-work-in-asp-dot-net-mvc-and-how-is-it-configured  
category: "asp.net mvc"  
tags: ["asp.net", "asp.net mvc", "mvc"]  
reading_time: 3 minutes  

---

# How does routing work in ASP.NET MVC and how is it configured?

How does [routing work in ASP.NET](https://www.mindstick.com/interview/34238/how-does-routing-work-in-asp-dot-net-mvc-explain-attribute-routing) [MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc) and how is it configured?

## Replies

### Reply by Aryan Kumar

[ASP.NET MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) [routing](https://www.mindstick.com/articles/11998/attribute-routing-in-asp-dot-net-mvc) is a pattern matching system that is responsible for mapping incoming browser requests to specified MVC controller actions. When a user requests a URL, the routing engine matches the URL against the registered routes and executes the controller action that is associated with the matching route.

Routing is configured in the RouteConfig class, which is located in the App_Start folder of the application. The RouteConfig class contains a collection of Route objects, which define the URL patterns and controller actions that are associated with each route.

To create a new route, you need to specify the following information:

- The URL pattern for the route.
- The controller name that is associated with the route.
- The action name that is associated with the route.
- Any parameters that are associated with the route.

For example, the following code defines a route that maps the URL /products/view/ to the ProductsController class and the View action:

Code snippet

```plaintext
routes.MapRoute(
    name: "Products",
    url: "products/view/{id}",
    defaults: new { controller = "Products", action = "View", id = UrlParameter.Optional }
);
```

When a user requests the URL /products/view/1, the routing engine will match the URL against the Products route and execute the ProductsController.View action. The ProductsController.View action will then render the view for the product with the ID of 1.

ASP.NET MVC also supports custom routing constraints, which can be used to restrict the values that are allowed for route parameters. For example, the following code defines a custom routing constraint that ensures that the value of the id parameter is a positive integer:

```plaintext
public class PositiveIntegerConstraint :
IRouteConstraint {
	public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
	{ int id; if (!int.TryParse(values[parameterName].AttemptedValue, out id) || id <= 0)
	{ return false; }
    return true;
}
}
```

The PositiveIntegerConstraint can then be used to restrict the values that are allowed for the id parameter in the Products route:

Code snippet

```plaintext
routes.MapRoute(
    name: "Products",
    url: "products/view/{id}",
    defaults: new { controller = "Products", action = "View", id = UrlParameter.Optional },
    constraints: new { id = new PositiveIntegerConstraint() }
);
```

This will ensure that the value of the id parameter is always a positive integer.

Routing is a powerful feature of ASP.NET MVC that allows you to decouple the URLs in your application from the controller actions that are used to process those requests. This makes your application more flexible and easier to maintain.


---

Original Source: https://www.mindstick.com/forum/158586/how-does-routing-work-in-asp-dot-net-mvc-and-how-is-it-configured

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
