Skip to content

UseStatusCodePagesWithReExecute() should ignore API requests #63936

@Atulin

Description

@Atulin

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

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 #62894 which solved #9039 could also be used to detect API routes and just skip them

Additional context

Workarounds exist, like

app.UseWhen(
    context => !context.Request.Path.StartsWithSegments("/api", StringComparison.OrdinalIgnoreCase),
    branch => branch.UseStatusCodePagesWithReExecute("/StatusCode/{0}");
);

or

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 #62894 proves it could just be done automagically.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewares

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions