mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 18:21:10 +00:00
Adds 404 NotFound page
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user