Adds 404 NotFound page

This commit is contained in:
BinaryAlley committed 2025-01-10 19:08:37 +02:00
1 parent 291c0768a5
commit b3ed6d1bb8
17 files changed
+1386 -3

No files matched your search

@@ -80,6 +80,8 @@ public class ExceptionHandlingMiddleware
// handle unauthorized errors by prompting the user to re-login
if (apiException.HttpStatusCode == HttpStatusCode.Unauthorized) // HTTP 401 Unauthorized actually means lack of valid authentication credentials (not logged in)
await HandleUnauthorizedAsync(context);
else if (apiException.HttpStatusCode == HttpStatusCode.NotFound) //
await HandleNotFoundAsync(context);
else if (apiException.HttpStatusCode == HttpStatusCode.Forbidden)
{
if (IsApiRequest(context))
@@ -105,6 +107,31 @@ public class ExceptionHandlingMiddleware
}
}
/// <summary>
/// Handles not found exceptions.
/// </summary>
private async Task HandleNotFoundAsync(HttpContext context)
{
if (IsApiRequest(context))
{
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
await WriteJsonResponseAsync(context, new
{
success = false,
errorMessage = "NotFound"
});
}
else
{
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
string culture = context.Request.RouteValues["culture"]?.ToString()?.ToLower() ?? "en-us";
string notFoundUrl = $"/{culture}/not-found";
_logger.LogInformation("Redirecting user to NotFound url");
context.Response.Redirect(notFoundUrl); // force an entire refresh of the current location, so that header and footer are re-rendered too
}
}
/// <summary>
/// Handles unauthorized exceptions.
/// </summary>
@@ -0,0 +1,44 @@
#region ========================================================================= USING =====================================================================================
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Web.Common.Middlewares;
/// <summary>
/// Middleware to handle requests towards resources that do not exist.
/// </summary>
public class NotFoundRedirectMiddleware
{
private readonly RequestDelegate _next;
/// <summary>
/// Initializes a new instance of the <see cref="NotFoundRedirectMiddleware"/> class.
/// </summary>
/// <param name="next">The next middleware in the request pipeline.</param>
public NotFoundRedirectMiddleware(RequestDelegate next)
{
_next = next;
}
/// <summary>
/// Handles incoming requests to resources that do not exist.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
public async Task InvokeAsync(HttpContext context)
{
await _next(context);
if (context.Response.StatusCode == 404 && !context.Response.HasStarted)
{
string culture = context.Request.RouteValues["culture"]?.ToString()?.ToLower() ?? "en-us";
string notFoundUrl = $"/{culture}/not-found";
// re-execute the request so the user gets the error page
string? originalPath = context.Request.Path.Value;
context.Items["originalPath"] = originalPath;
context.Request.Path = notFoundUrl;
await _next(context);
}
}
}
@@ -0,0 +1,23 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.Middlewares;
using Microsoft.AspNetCore.Builder;
#endregion
namespace Lumina.Presentation.Web.Common.Utilities;
/// <summary>
/// Extension methods for making it easier for <see cref="NotFoundRedirectMiddleware"/> to be added the middleware.
/// </summary>
public static class NotFoundRedirectMiddlewareUtilities
{
/// <summary>
/// Adds the <see cref="NotFoundRedirectMiddleware"/> to the application's request pipeline.
/// Redirects requests to resources that do not exist.
/// </summary>
/// <param name="builder">The <see cref="IApplicationBuilder"/> used to configure the application's request pipeline.</param>
/// <returns>The <see cref="IApplicationBuilder"/> for further configuration.</returns>
public static IApplicationBuilder UseNotFoundRedirect(this IApplicationBuilder builder)
{
return builder.UseMiddleware<NotFoundRedirectMiddleware>();
}
}