Featured Article

Master .NET Core MVC Routing: The Ultimate Guide to SEO-Friendly URL Design

Kenneth Jul 13, 2026

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.

Routing in MVC - ASP.NET Core Demystified
Routing in MVC - ASP.NET Core Demystified

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.

APIs Rest + NET CoreπŸ‘¨β€πŸ’»πŸ’»
APIs Rest + NET CoreπŸ‘¨β€πŸ’»πŸ’»

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.

Attribute Routing in Dotnet Core MVC | How to use attribute routing in ASP.NET Core
Attribute Routing in Dotnet Core MVC | How to use attribute routing in ASP.NET Core

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

What is ASP.NET Core MVC? – The Complete Guide 2023
What is ASP.NET Core MVC? – The Complete Guide 2023

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

Complete ASP.NET Core MVC 6: A Project guide 2025
Complete ASP.NET Core MVC 6: A Project guide 2025

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]
Generating Custom URL Routing in ASP.NET Core MVC [Change Routing]

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

ROUTING IN ASP.NET CORE | MVC | SHARIQ MANZOOR
ROUTING IN ASP.NET CORE | MVC | SHARIQ MANZOOR
How To Create Routing In .NET Core?
How To Create Routing In .NET Core?
What is Endpoint Routing, Implementing it from start [with codes]
What is Endpoint Routing, Implementing it from start [with codes]
CodeIgniter Introduction
CodeIgniter Introduction
Complete ASP.NET Core MVC 6 with Real-World Projects
Complete ASP.NET Core MVC 6 with Real-World Projects
ASP.NET MVC CORE DEVELOPMENT COURSE FOR SUMMER COURSE
ASP.NET MVC CORE DEVELOPMENT COURSE FOR SUMMER COURSE
Complete ASP.NET Core MVC 6 with Real-World Projects
Complete ASP.NET Core MVC 6 with Real-World Projects

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.