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

- **ID:** `dotnet/aspnet-core-middleware-ordering`
- **Domain:** dotnet
- **Category:** config_error
- **Verification:** ai_generated
- **Fix Rate:** 95%

## Root Cause

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

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 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 | — | — |

## Workarounds

1. **Ensure middleware order: app.UseRouting(); app.UseEndpoints(endpoints => { ... });** (95% success)
   ```
   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.** (90% success)
   ```
   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.** (85% success)
   ```
   For minimal APIs, ensure app.MapGet/Post are called after UseRouting and before UseEndpoints.
   ```

## Dead Ends

- **** — Removing UseRouting entirely breaks endpoint routing for MVC or minimal APIs. (95% fail)
- **** — Adding duplicate UseRouting calls causes ambiguous routing state. (90% fail)
- **** — Moving UseEndpoints before UseRouting in the same method doesn't fix order in different Configure blocks. (85% fail)
