`UseStatusCodePagesWithReExecute()` should ignore API requests
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Is your feature request related to a problem? Please describe the problem.
```cs
app.UseStatusCodePagesWithReExecute("/StatusCode/{0}");
```
Whenever a non-200 status code is returned by an API controller (or Minimal API endpoint) this middleware triggers just like it would for Razor Pages or MVC. That makes the API return a whole rendered HTML page for something like a `404 Not Found` result, instead.
### Describe the solution you'd like
I'd imagine a solution like that in https://github.com/dotnet/aspnetcore/pull/62894 which solved https://github.com/dotnet/aspnetcore/issues/9039 could also be used to detect API routes and just skip them
### Additional context
Workarounds exist, like
```cs
app.UseWhen(
context => !context.Request.Path.StartsWithSegments("/api", StringComparison.OrdinalIgnoreCase),
branch => branch.UseStatusCodePagesWithReExecute("/StatusCode/{0}");
);
```
or
```cs
app.UseWhen(
context =>
{
var accept = context.Request.Headers.Accept.ToString();
return
string.IsNullOrEmpty(accept) ||
accept.Contains("text/html", StringComparison.OrdinalIgnoreCase);
},
branch => branch.UseStatusCodePagesWithReExecute("/StatusCode/{0}");
);
```
exist, but they either need a magic string or a bunch of code, while https://github.com/dotnet/aspnetcore/pull/62894 proves it could just be done automagically.
Contributor guide
Assessment
This issue has not been assessed yet.