In my .NET 7 web site, I’ve set up the processing pipeline to use UseStatusCodePagesWithReExecute
but with a registered middleware in the pipeline returns 404, it correctly returns the 404.cshtml page, but it doesn’t seem to reprocess the entire pipeline.
For example, I have a need to render a 404.cshtml page, but I have to change the response.StatusCode = 400
instead of 404
.
During the pipeline construction, I do this:
app
.Use( async ( context, next ) => {
if ( new[] { 500, 401, 403, 404 }.Contains( context.Response.StatusCode ) )
{
Console.WriteLine( $"app.Use (before) StatusCode: {context.Response.StatusCode}, {context.Request.Path}" );
}
await next();
} )
.UseExceptionHandler( "/errors/handler" )
.UseStatusCodePagesWithReExecute( "/errors/{0}" );
// register more middlewares, one which could return 404, psuedo is below...
app.Use( async ( context, next ) =>
{
if ( context.Request.Path.StartsWithSegments( "/app/channel-homex" ) )
{
context.Response.StatusCode = 404;
return;
}
await next();
} ).Use( async ( context, next ) =>
{
if ( new[] { 500, 401, 403, 404 }.Contains( context.Response.StatusCode ) )
{
Console.WriteLine( $"app.Use (after) StatusCode: {context.Response.StatusCode}, {context.Request.Path}" );
}
await next();
} );
As I understand it, when I do context.Response.StatusCode = 404; return;
it is supposed to start the pipeline over, however, I only get the ‘after’ output in the console instead of both the ‘before’ and the ‘after’.
Am I misunderstanding pipelines or how to process return
vs await next()
in my middleware implementations?