You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从.NET Core 2.1升级到3.0:三种路由方法的差异解析

Understanding MapControllerRoute, MapDefaultControllerRoute, and MapControllers in .NET Core 3.0+

Hey there! Let's clear up the confusion around these three routing methods, especially focusing on MapControllers since that's what's tripping you up.

First, let's recap the ones you already have some context on:

  • MapDefaultControllerRoute: As you noted, this is just a convenient shorthand for the classic default route. It sets up the pattern {controller=Home}/{action=Index}/{id?} out of the box, which is perfect for traditional MVC apps where you want URLs to map directly to controller and action names.
  • MapControllerRoute: This is the flexible, customizable version. Use this when you need to define named, custom global routes—like if you're working with areas, localized URLs, or any pattern that doesn't fit the default template. For example:
    endpoints.MapControllerRoute(
        name: "areaRoute",
        pattern: "{area:exists}/{controller=Home}/{action=Index}"
    );
    

The real question: What does MapControllers do?

The docs' description ("adds controller action endpoints without specifying any routes") sounds abstract, but it's actually straightforward once you put it in context.

MapControllers is built for apps that use attribute routing exclusively. That means you don't rely on any global route patterns—every single controller action defines its own URL pattern via attributes like [Route], [HttpGet], [HttpPost], etc.

Example in action

Suppose you have a Web API controller like this:

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    // Accessible via GET /api/products/{id}
    [HttpGet("{id:int}")]
    public IActionResult GetById(int id)
    {
        // ...
    }

    // Accessible via POST /api/products
    [HttpPost]
    public IActionResult Create(ProductDto product)
    {
        // ...
    }
}

Here, all routing rules are defined right on the actions and controller. There's no need for a global default route because each endpoint already knows exactly how to be reached.

In your startup code, you'd use MapControllers like this:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers(); // No global route needed—attributes handle all routing
});

Key distinctions from the other methods

  • No global route template is applied. Any action without routing attributes won't be accessible via HTTP at all.
  • It's the go-to choice for modern Web APIs (especially when paired with [ApiController]) where you want precise control over your URL structure.
  • It plays nicely with other endpoint types too—if you're mixing Web API controllers with Razor Pages or minimal APIs, MapControllers lets you register your attribute-routed controllers without interfering with other routes.

Quick decision guide

  • Use MapDefaultControllerRoute for traditional MVC apps with views, where you want URLs to follow the /Controller/Action/Id pattern.
  • Use MapControllerRoute when you need custom global route rules (e.g., areas, multi-language routes).
  • Use MapControllers for Web APIs that rely solely on attribute routing, or when you don't want any global route conventions applied.

内容的提问来源于stack exchange,提问作者xavier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:46:43