dotnet config_error ai_generated true

System.InvalidOperationException: 必须在中间件管道中先调用 'UseRouting' 方法,然后才能调用 'UseEndpoints'。

System.InvalidOperationException: The 'UseRouting' method must be called before 'UseEndpoints' in the middleware pipeline.

ID: dotnet/aspnet-core-middleware-ordering

其他格式: JSON · Markdown 中文 · English
95%修复率
88%置信度
1证据数
2023-01-10首次发现

版本兼容性

版本状态引入弃用备注
ASP.NET Core 3.1.x active
ASP.NET Core 5.0.x active
ASP.NET Core 6.0.x active
ASP.NET Core 7.0.x active
ASP.NET Core 8.0.x active

根因分析

ASP.NET Core 中间件管道排序违规:UseRouting 必须在 UseEndpoints 之前调用,但顺序颠倒或缺失。

English

ASP.NET Core middleware pipeline ordering violation: UseRouting must precede UseEndpoints, but they are reversed or missing.

generic

官方文档

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-8.0#order

解决方案

  1. Ensure middleware order: app.UseRouting(); app.UseEndpoints(endpoints => { ... });
  2. Use app.UseRouting() and app.UseEndpoints() in the same Configure method, not split across partial classes.
  3. For minimal APIs, ensure app.MapGet/Post are called after UseRouting and before UseEndpoints.

无效尝试

常见但无效的做法:

  1. 95% 失败

    Removing UseRouting entirely breaks endpoint routing for MVC or minimal APIs.

  2. 90% 失败

    Adding duplicate UseRouting calls causes ambiguous routing state.

  3. 85% 失败

    Moving UseEndpoints before UseRouting in the same method doesn't fix order in different Configure blocks.