From f3f8739b8b0c4290d9712030dfa3cea49d5f90b2 Mon Sep 17 00:00:00 2001 From: Alexandre Pominville Date: Fri, 13 Mar 2026 16:24:52 -0400 Subject: [PATCH 1/3] Refactor NotFoundHandlerMiddleware to handle responses on starting --- .../Initialization/NotFoundHandlerMiddleware.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Geta.NotFoundHandler/Infrastructure/Initialization/NotFoundHandlerMiddleware.cs b/src/Geta.NotFoundHandler/Infrastructure/Initialization/NotFoundHandlerMiddleware.cs index fec19c33..b69fecb4 100644 --- a/src/Geta.NotFoundHandler/Infrastructure/Initialization/NotFoundHandlerMiddleware.cs +++ b/src/Geta.NotFoundHandler/Infrastructure/Initialization/NotFoundHandlerMiddleware.cs @@ -19,8 +19,12 @@ public NotFoundHandlerMiddleware(RequestDelegate next) public async Task InvokeAsync(HttpContext context, RequestHandler requestHandler) { await _next(context); - - requestHandler.Handle(context); + + context.Response.OnStarting(() => + { + requestHandler.Handle(context); + return Task.CompletedTask; + }); } } } From f93e9bdb26025b3a3cb4f5c30b6d5ff92a7d9528 Mon Sep 17 00:00:00 2001 From: Alexandre Pominville Date: Mon, 16 Mar 2026 13:32:52 -0400 Subject: [PATCH 2/3] Refactor NotFoundHandlerMiddleware to use IMiddleware and streamline response handling. Removed condition for forced http 404 when allowing HTTP 200 as active code status in the options. --- .../Core/RequestHandler.cs | 12 ----------- .../ServiceCollectionExtensions.cs | 1 + .../NotFoundHandlerMiddleware.cs | 20 +++++++++---------- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/src/Geta.NotFoundHandler/Core/RequestHandler.cs b/src/Geta.NotFoundHandler/Core/RequestHandler.cs index 1ce8b66f..4377685a 100644 --- a/src/Geta.NotFoundHandler/Core/RequestHandler.cs +++ b/src/Geta.NotFoundHandler/Core/RequestHandler.cs @@ -95,18 +95,6 @@ public virtual void Handle(HttpContext context) context .Redirect(newUrl.NewUrl, newUrl.RedirectType); } - else if (canHandleRedirect && newUrl.State == (int)RedirectState.Deleted) - { - LogDebug("Handled deleted URL", context); - - SetStatusCodeAndShow404(context, 410); - } - else - { - LogDebug("Not handled. Current URL is ignored or no redirect found.", context); - - SetStatusCodeAndShow404(context); - } MarkHandled(context); } diff --git a/src/Geta.NotFoundHandler/Infrastructure/Configuration/ServiceCollectionExtensions.cs b/src/Geta.NotFoundHandler/Infrastructure/Configuration/ServiceCollectionExtensions.cs index 02a79a4e..92802ffb 100644 --- a/src/Geta.NotFoundHandler/Infrastructure/Configuration/ServiceCollectionExtensions.cs +++ b/src/Geta.NotFoundHandler/Infrastructure/Configuration/ServiceCollectionExtensions.cs @@ -41,6 +41,7 @@ public static IServiceCollection AddNotFoundHandler( services.AddSingleton(s => s.GetRequiredService()); services.AddSingleton(); services.AddTransient(); + services.AddTransient(); services.AddSingleton>(x => x.GetService); services.AddTransient(); diff --git a/src/Geta.NotFoundHandler/Infrastructure/Initialization/NotFoundHandlerMiddleware.cs b/src/Geta.NotFoundHandler/Infrastructure/Initialization/NotFoundHandlerMiddleware.cs index b69fecb4..bf8a4f36 100644 --- a/src/Geta.NotFoundHandler/Infrastructure/Initialization/NotFoundHandlerMiddleware.cs +++ b/src/Geta.NotFoundHandler/Infrastructure/Initialization/NotFoundHandlerMiddleware.cs @@ -7,24 +7,24 @@ namespace Geta.NotFoundHandler.Infrastructure.Initialization { - public class NotFoundHandlerMiddleware + public class NotFoundHandlerMiddleware : IMiddleware { - private readonly RequestDelegate _next; + private readonly RequestHandler _requestHandler; - public NotFoundHandlerMiddleware(RequestDelegate next) + public NotFoundHandlerMiddleware(RequestHandler requestHandler) { - _next = next; + _requestHandler = requestHandler; } - public async Task InvokeAsync(HttpContext context, RequestHandler requestHandler) + public async Task InvokeAsync(HttpContext context, RequestDelegate next) { - await _next(context); - - context.Response.OnStarting(() => + context.Response.OnStarting(state => { - requestHandler.Handle(context); + _requestHandler.Handle((HttpContext)state); return Task.CompletedTask; - }); + }, context); + + await next(context); } } } From b4c3756d026ca238755d1763cc1108b0c973f6c8 Mon Sep 17 00:00:00 2001 From: Alexandre Pominville Date: Mon, 16 Mar 2026 17:07:18 -0400 Subject: [PATCH 3/3] Enhance RequestHandler to correctly handle 404 URIs during status code re-execution. Added logic to construct the not found URI from original request details when available. --- .../Core/RequestHandler.cs | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Geta.NotFoundHandler/Core/RequestHandler.cs b/src/Geta.NotFoundHandler/Core/RequestHandler.cs index 4377685a..5fbda581 100644 --- a/src/Geta.NotFoundHandler/Core/RequestHandler.cs +++ b/src/Geta.NotFoundHandler/Core/RequestHandler.cs @@ -7,6 +7,7 @@ using Geta.NotFoundHandler.Core.Suggestions; using Geta.NotFoundHandler.Infrastructure.Configuration; using Geta.NotFoundHandler.Infrastructure.Web; +using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Extensions; using Microsoft.Extensions.Logging; @@ -69,7 +70,24 @@ public virtual void Handle(HttpContext context) LogDebug("Handling 404 request.", context); - var notFoundUri = new Uri(context.Request.GetDisplayUrl()); + Uri notFoundUri; + + if (context.Features.Get() is StatusCodeReExecuteFeature statusCodeReExecuteFeature) + { + var request = context.Request; + var absoluteUrl = $"{request.Scheme}://{request.Host}{statusCodeReExecuteFeature.OriginalPathBase}{statusCodeReExecuteFeature.OriginalPath}{statusCodeReExecuteFeature.OriginalQueryString}"; + + if (!Uri.TryCreate(absoluteUrl, UriKind.Absolute, out notFoundUri)) + { + // Fallback to current request URL if construction fails + notFoundUri = new Uri(context.Request.GetDisplayUrl()); + } + } + else + { + notFoundUri = new Uri(context.Request.GetDisplayUrl()); + } + if (IsResourceFile(notFoundUri)) {