diff --git a/src/Geta.NotFoundHandler/Core/RequestHandler.cs b/src/Geta.NotFoundHandler/Core/RequestHandler.cs index 1ce8b66..5fbda58 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)) { @@ -95,18 +113,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 02a79a4..92802ff 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 fec19c3..bf8a4f3 100644 --- a/src/Geta.NotFoundHandler/Infrastructure/Initialization/NotFoundHandlerMiddleware.cs +++ b/src/Geta.NotFoundHandler/Infrastructure/Initialization/NotFoundHandlerMiddleware.cs @@ -7,20 +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(state => + { + _requestHandler.Handle((HttpContext)state); + return Task.CompletedTask; + }, context); - requestHandler.Handle(context); + await next(context); } } }