In the dynamic world of web development, understanding routing is crucial, especially when working with ASP.NET Core MVC. Routing is the process of mapping HTTP requests to controller actions, enabling a clear and logical navigation structure in your application. Let's delve into the intricacies of .NET Core MVC routing to help you build robust and maintainable web applications.

By default, .NET Core MVC uses attribute routing, which associates HTTP verbs and URLs with controller actions via attributes. However, conventional routing, based on routes defined in the Route.config file, is also supported for backward compatibility. In this article, we'll explore both routing approaches, their configurations, and best practices to help you make the most of your .NET Core MVC routing.

Understanding the Routing Process
The routing process in .NET Core MVC involves several key components: routes, route data, route handlers, and route constraints. When a request arrives, the routing middleware selects a route that matches the request's URL, HTTP method, and any route constraints.

The selected route is then used to create a RouteData object, which contains information about the route, such as the controller name, action name, and any route parameters. This data is passed to the Controller to dispatch the action that will handle the request.
Conventional Routing

Conventional routing is configured using the ConfigureRoutes method in the Startup.cs file. It uses URLs and HTTP methods to define routes and map them to controller actions. Here's an example of conventional routing configuration:
app.UseStaticFiles();
app.UseRouting();
// Define routes
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
The MapControllerRoute method defines a route with the name "default". The pattern {controller=Home}/{action=Index}/{id?} maps requests to the specified controller and action, with an optional id parameter. If no controller or action is specified, it defaults to the Home controller and Index action.
Attribute Routing

Attribute routing is a more flexible and fine-grained approach, allowing you to define routes directly on controller actions using attributes. This approach is encouraged in .NET Core, as it promotes clear and concise routing definitions. Here's an example of attribute routing:
[ApiController]
[Route("[controller]")]
public class HomeController : ControllerBase
{
[HttpGet]
public IActionResult Get() => Ok("Hello from Home controller!");
[HttpGet("{id}")]
public IActionResult Get(int id) => Ok($"You requested {id}");
}
In this example, we've defined a Home controller with two actions: Get and another Get method accepting an id parameter. The Route attribute defines the base route for the controller, and the HttpGet attributes specify the HTTP method for each action.
Route Constraints and Defaults
![Generating Custom URL Routing in ASP.NET Core MVC [Change Routing]](https://i.pinimg.com/originals/30/96/70/30967091fef1ed4ed0ed05e9165db49d.jpg)
Route constraints enable you to match URLs with specific patterns, while route defaults provide fallbacks for route parameters. Here's how to use them effectively:
Route Constraints


![What is Endpoint Routing, Implementing it from start [with codes]](https://i.pinimg.com/originals/8b/e0/8f/8be08f5e24361606390a79b453d04943.png)




Route constraints allow you to enforce specific rules on route parameters, ensuring that only valid values are accepted. Here's an example of using route constraints in attribute routing:
[HttpGet("{id:min(1)}")]
public IActionResult Get(int id) => Ok($"You requested valid id: {id}");
In this example, the {id:min(1)} route constraint ensures that the id parameter is greater than or equal to 1.
Route Defaults
Route defaults provide fallback values for route parameters when they are not explicitly specified in the URL. Here's an example of using route defaults in conventional routing:
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}",
defaults: new { id = 1 });
In this example, if no id parameter is specified in the URL, the id route parameter will default to 1.
Mastering .NET Core MVC routing will empower you to build versatile and efficient web applications. By understanding and leveraging both conventional and attribute routing, along with route constraints and defaults, you'll be well-equipped to tackle even the most complex routing scenarios. Embrace the journey of discovering and implementing effective routing strategies to enhance your .NET Core MVC development skills.