Skip to content

Commit

Permalink
Do not redirect to 404 with 302
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Nielsen committed Dec 22, 2023
1 parent 5333a96 commit 619c3d6
Showing 1 changed file with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.StaticFiles;
Expand Down Expand Up @@ -169,7 +170,7 @@ public static void UsePageNotFound(this IApplicationBuilder application)
//get new path
var pageNotFoundPath = "/page-not-found";
//re-execute request with new path
context.HttpContext.Response.Redirect(context.HttpContext.Request.PathBase + pageNotFoundPath);
await CreateHandler(pageNotFoundPath, null)(context);
}
finally
{
Expand Down Expand Up @@ -545,4 +546,60 @@ public static void UseNopWebMarkupMin(this IApplicationBuilder application)

application.UseWebMarkupMin();
}

/// <summary>
/// Creates a request handling method based on specified paths and a next request delegate.
/// It was inspired by UseStatusCodePagesWithReExecute method from Microsoft.AspNetCore.Builder.StatusCodePagesExtensions class.
/// </summary>
/// <param name="pathFormat">A string representing the path format.</param>
/// <param name="queryFormat">A string representing the query format.</param>
/// <param name="next">The next request delegate in the pipeline.</param>
/// <returns>A function that handles the status code context.</returns>
private static Func<StatusCodeContext, Task> CreateHandler(string pathFormat, string queryFormat, RequestDelegate next = null)
{
return (async context =>
{
var statusCode = context.HttpContext.Response.StatusCode;
var pathString = new PathString(string.Format(CultureInfo.InvariantCulture, pathFormat, statusCode));
var str = queryFormat == null ? null : string.Format(CultureInfo.InvariantCulture, queryFormat, statusCode);
var queryString = queryFormat == null ? QueryString.Empty : new QueryString(str);
var originalPath = context.HttpContext.Request.Path;
var originalQueryString = context.HttpContext.Request.QueryString;
var routeValuesFeature = context.HttpContext.Features.Get<IRouteValuesFeature>();
context.HttpContext.Features.Set<IStatusCodeReExecuteFeature>(new StatusCodeReExecuteFeature
{
OriginalPathBase = context.HttpContext.Request.PathBase.Value!,
OriginalPath = originalPath.Value!,
OriginalQueryString = (originalQueryString.HasValue ? originalQueryString.Value : null),
Endpoint = context.HttpContext.GetEndpoint(),
RouteValues = routeValuesFeature?.RouteValues
});
context.HttpContext.SetEndpoint(null);
if (routeValuesFeature != null)
routeValuesFeature.RouteValues = null!;
context.HttpContext.Request.Path = pathString;
context.HttpContext.Request.QueryString = queryString;
try
{
if (next != null)
{
await next(context.HttpContext);
originalPath = new PathString();
originalQueryString = new QueryString();
}
else
{
await context.Next(context.HttpContext);
originalPath = new PathString();
originalQueryString = new QueryString();
}
}
finally
{
context.HttpContext.Request.QueryString = originalQueryString;
context.HttpContext.Request.Path = originalPath;
context.HttpContext.Features.Set<IStatusCodeReExecuteFeature>(null);
}
});
}
}

0 comments on commit 619c3d6

Please sign in to comment.