Routing in ASP.NET Core
Routing is the process of mapping incoming HTTP requests to the code that handles them. In ASP.NET Core, routing is a fundamental part of building web APIs and MVC applications.
Key Concepts
- Endpoint Routing – A unified routing system introduced in ASP.NET Core 3.0.
- Conventional Routing – Defines routes using patterns, typically at startup.
- Attribute Routing – Allows you to define routes directly on controller actions.
- Route Constraints – Restrict how routes match URL segments.
Getting Started
C#
JSON
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
var app = builder.Build();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
app.Run();
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Try it Now
Use the interactive console below to test a simple route pattern.