Skip to content

Controller Action that returns non-success HTTP status code is invoked twice #16731

@huan086

Description

@huan086

Describe the bug

A controller action that returns non-success HTTP status code that is re-executed by UseStatusCodePagesWithReExecute is invoked twice.

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of ASP.NET Core 3.0
  2. Run this code
        public async Task<IActionResult> IndexAsync(string returnUrl)
        {
            if (!string.IsNullOrEmpty(returnUrl) && returnUrl[0] != '/')
            {
                return this.BadRequest();
            }

            return this.View();
        }
            app.UseStatusCodePagesWithReExecute("/error", "?{0}");
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(name: "error", pattern: "error", defaults: new { controller = "Error", action = "Error" });
                endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
            });
  1. With these arguments returnUrl = "a"
  2. See error
    See that IndexAsync is called initially with returnUrl == "a", and then UseStatusCodePagesWithReExecute triggered a second request. However, instead of hitting the ErrorController, the same controller is hit and IndexAsync is called with returnUrl == null

Expected behavior

Expected to have app.UseStatusCodePagesWithReExecute("/error", "?{0}"); routed to ErrorController

Screenshots

None

Additional context

Original report #11233
Fixed by c54a720
Broken again by #13068. In this commit, it reverted the change to clear endpoint after the endpoint middleware finished running. However, it only takes care of thrown exception, but does not take care of controller returning status code result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions