Aspnetcore: preview9 UseStatusCodePagesWithReExecute stopped working

Created on 5 Sep 2019  路  14Comments  路  Source: dotnet/aspnetcore

Currently the following code stopped working with preview9:

            app.UseStatusCodePagesWithReExecute("/Status", "?code={0}");

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapBlazorHub();
                endpoints.MapDefaultControllerRoute();
                endpoints.MapRazorPages();
            });

I would've hoped that a authentication failure 401 would run the Status page with code=401 unfortunatly this stopped working in preview9 (preview8 worked)

Done area-middleware bug good first issue help wanted

Most helpful comment

Original issue: https://github.com/aspnet/AspNetCore/issues/11233
Mitigation:

            app.UseStatusCodePagesWithReExecute("/Status", "?code={0}");

            app.Use((context, next) => {
                context.SetEndpoint(null);
                return next();
            });

            app.UseRouting();

All 14 comments

@HaoK ?

I'm not super familiar with status code pages, but it looks like there was a change in preview 9 here: https://github.com/aspnet/AspNetCore/commit/1f0641f5c0c8552f9605ff782ba1a27c06f4cedf#diff-a68f1924d718fb2c1219de059d8adab6

@NTaylorMullen any thoughts about whether this might affect things in this way?

We fixed UseExceptionHandlerWithReExecute but overlooked UseStatusCodePagesWithReExecute. This one also needs context.SetEndpoint(endpoint: null);

Original issue: https://github.com/aspnet/AspNetCore/issues/11233
Mitigation:

            app.UseStatusCodePagesWithReExecute("/Status", "?code={0}");

            app.Use((context, next) => {
                context.SetEndpoint(null);
                return next();
            });

            app.UseRouting();

shouldn't this be attached to the 3.0.0 milestone?

It's too late for 3.0. We'll reconsider for 3.1.

well isn't this a bug?! i.e. should at least be in 3.0.1?

Bugs don't automatically qualify for patching, they have to be pretty severe and hard to work around. We'll decide if a patch backport is feasible after we see what the fix looks like in 3.1.

@shirhatti @Pilchie looks like a 3.0 regression we'll need to address in 3.1.

As there is a workaround, and 3.1 is shipping this year, I expect we won't backport since it won't really get the fix out any faster :).

We'll look at this, but it could be a useful first PR if anyone is interested. We just need to null out the Endpoint property on the IEndpointFeature in the request features collection to force endpoint routing to run.

@shirhatti says he wants to take a look ;).

I have encountered this issue in my production environment. I did a simple test, create a new MVC project (VS2019 16.3.1 + .NET Core 3.0 GA).

Change HomeController code to:

public IActionResult Index(int id = 0)
{
    if (id == 1)
    {
        return NotFound();
    }
    return View();
}

[Route("/error")]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error(int? statusCode = null)
{
    return Content($"Test Error Action: {statusCode}");
}

Startup.cs:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    //app.UseStatusCodePages();
    //app.UseExceptionHandler("/error");
    app.UseStatusCodePagesWithReExecute("/error", "?statusCode={0}");

    app.UseHttpsRedirection();
    app.UseStaticFiles();

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}

Then, make a request to /Home/Index?id=1

Expected Result:
Showing "Test Error Action: 404"

Actual Result:
The request never hit "Error" action. It goes back to "Index" again, but the id parameter was set to 0.

It did hit NotFound()
image

But then instead of executing /error/statusCode=404, it hit Index action again with id=0

image

@EdiWang see the workaround above https://github.com/aspnet/AspNetCore/issues/13715#issuecomment-528929683

@Tratcher thanks, the workaround fixed the problem.

Was this page helpful?
0 / 5 - 0 ratings