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
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.
官方文档
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-8.0#order解决方案
-
Ensure middleware order: app.UseRouting(); app.UseEndpoints(endpoints => { ... }); -
Use app.UseRouting() and app.UseEndpoints() in the same Configure method, not split across partial classes.
-
For minimal APIs, ensure app.MapGet/Post are called after UseRouting and before UseEndpoints.
无效尝试
常见但无效的做法:
-
95% 失败
Removing UseRouting entirely breaks endpoint routing for MVC or minimal APIs.
-
90% 失败
Adding duplicate UseRouting calls causes ambiguous routing state.
-
85% 失败
Moving UseEndpoints before UseRouting in the same method doesn't fix order in different Configure blocks.