diff --git a/src/Lumina.Presentation.Web/Common/Middlewares/ExceptionHandlingMiddleware.cs b/src/Lumina.Presentation.Web/Common/Middlewares/ExceptionHandlingMiddleware.cs index 56621ec3..05c52008 100644 --- a/src/Lumina.Presentation.Web/Common/Middlewares/ExceptionHandlingMiddleware.cs +++ b/src/Lumina.Presentation.Web/Common/Middlewares/ExceptionHandlingMiddleware.cs @@ -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 } } + /// + /// Handles not found exceptions. + /// + 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 + } + } + /// /// Handles unauthorized exceptions. /// diff --git a/src/Lumina.Presentation.Web/Common/Middlewares/NotFoundRedirectMiddleware.cs b/src/Lumina.Presentation.Web/Common/Middlewares/NotFoundRedirectMiddleware.cs new file mode 100644 index 00000000..c2568966 --- /dev/null +++ b/src/Lumina.Presentation.Web/Common/Middlewares/NotFoundRedirectMiddleware.cs @@ -0,0 +1,44 @@ +#region ========================================================================= USING ===================================================================================== +using Microsoft.AspNetCore.Http; +using System.Threading.Tasks; +#endregion + +namespace Lumina.Presentation.Web.Common.Middlewares; + +/// +/// Middleware to handle requests towards resources that do not exist. +/// +public class NotFoundRedirectMiddleware +{ + private readonly RequestDelegate _next; + + /// + /// Initializes a new instance of the class. + /// + /// The next middleware in the request pipeline. + public NotFoundRedirectMiddleware(RequestDelegate next) + { + _next = next; + } + + /// + /// Handles incoming requests to resources that do not exist. + /// + /// The for the current request. + 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); + } + } +} diff --git a/src/Lumina.Presentation.Web/Common/Utilities/NotFoundRedirectMiddlewareUtilities.cs b/src/Lumina.Presentation.Web/Common/Utilities/NotFoundRedirectMiddlewareUtilities.cs new file mode 100644 index 00000000..e6ac8610 --- /dev/null +++ b/src/Lumina.Presentation.Web/Common/Utilities/NotFoundRedirectMiddlewareUtilities.cs @@ -0,0 +1,23 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Presentation.Web.Common.Middlewares; +using Microsoft.AspNetCore.Builder; +#endregion + +namespace Lumina.Presentation.Web.Common.Utilities; + +/// +/// Extension methods for making it easier for to be added the middleware. +/// +public static class NotFoundRedirectMiddlewareUtilities +{ + /// + /// Adds the to the application's request pipeline. + /// Redirects requests to resources that do not exist. + /// + /// The used to configure the application's request pipeline. + /// The for further configuration. + public static IApplicationBuilder UseNotFoundRedirect(this IApplicationBuilder builder) + { + return builder.UseMiddleware(); + } +} diff --git a/src/Lumina.Presentation.Web/Controllers/HomeController.cs b/src/Lumina.Presentation.Web/Controllers/HomeController.cs index 1440fc03..fd9dcf02 100644 --- a/src/Lumina.Presentation.Web/Controllers/HomeController.cs +++ b/src/Lumina.Presentation.Web/Controllers/HomeController.cs @@ -12,7 +12,7 @@ namespace Lumina.Presentation.Web.Controllers; /// Controller for home page. /// [Authorize] -[Route("")] // randle root path - this is needed because app.UseCultureRedirect will redirect to the correct route +[Route("")] // handle root path - this is needed because app.UseCultureRedirect will redirect to the correct route [Route("{culture}")] // culture-specific routes public class HomeController : Controller { @@ -54,4 +54,13 @@ public class HomeController : Controller { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } + + /// + /// Displays the view for errors. + /// + [HttpGet("not-found")] + public IActionResult NotFoundView() + { + return View("/Views/Shared/NotFound.cshtml"); + } } diff --git a/src/Lumina.Presentation.Web/Program.cs b/src/Lumina.Presentation.Web/Program.cs index 1ce067a1..fdbfdb5e 100644 --- a/src/Lumina.Presentation.Web/Program.cs +++ b/src/Lumina.Presentation.Web/Program.cs @@ -62,13 +62,14 @@ public class Program WebApplication app = builder.Build(); + app.UseNotFoundRedirect(); app.UseRequestLocalization(); app.UseSerilogRequestLogging(); // configure the HTTP request pipeline. if (!app.Environment.IsDevelopment() && Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") != "true") { - app.UseExceptionHandler("/Home/Error"); + app.UseExceptionHandler("/home/error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } diff --git a/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.de-DE.resx b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.de-DE.resx new file mode 100644 index 00000000..3a1919ca --- /dev/null +++ b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.de-DE.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Versuche nicht, diese Seite zu finden. Das ist unmöglich. Stattdessen... versuche nur die Wahrheit zu erkennen: + + + Es gibt keine Seite! + + \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.en-US.resx b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.en-US.resx new file mode 100644 index 00000000..da7e5bf3 --- /dev/null +++ b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.en-US.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Do not try and find this page. That's impossible. Instead... only try to realize the truth: + + + There is no page! + + \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.es-ES.resx b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.es-ES.resx new file mode 100644 index 00000000..806014ea --- /dev/null +++ b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.es-ES.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + No intentes encontrar esta página. Es imposible. En su lugar... solo trata de comprender la verdad: + + + ¡No hay ninguna página! + + \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.fr-FR.resx b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.fr-FR.resx new file mode 100644 index 00000000..a4512708 --- /dev/null +++ b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.fr-FR.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + N'essayez pas de trouver cette page. C'est impossible. Au lieu de cela... essayez seulement de réaliser la vérité: + + + Il n'y a pas de page! + + \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.it-IT.resx b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.it-IT.resx new file mode 100644 index 00000000..a0be42b9 --- /dev/null +++ b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.it-IT.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Non provare a trovare questa pagina. È impossibile. Invece... prova solo a realizzare la verità: + + + Non esiste alcuna pagina! + + \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.ja-JP.resx b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.ja-JP.resx new file mode 100644 index 00000000..2c020d89 --- /dev/null +++ b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.ja-JP.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + このページを探そうとしないで。それは不可能だ。代わりに...真実に気づくだけでいい: + + + ページなんてないのだ! + + \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.resx b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.resx new file mode 100644 index 00000000..da7e5bf3 --- /dev/null +++ b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Do not try and find this page. That's impossible. Instead... only try to realize the truth: + + + There is no page! + + \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.ro-RO.resx b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.ro-RO.resx new file mode 100644 index 00000000..140a18a5 --- /dev/null +++ b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.ro-RO.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Nu încerca să găsești această pagină. Este imposibil. În schimb... încearcă doar să realizezi adevărul: + + + Nu există o pagină! + + \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.ru-RU.resx b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.ru-RU.resx new file mode 100644 index 00000000..cbf49d08 --- /dev/null +++ b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.ru-RU.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Не пытайся найти эту страницу. Это невозможно. Вместо этого... попытайся осознать истину: + + + Страницы не существует! + + \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.zh-CN.resx b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.zh-CN.resx new file mode 100644 index 00000000..f35d1192 --- /dev/null +++ b/src/Lumina.Presentation.Web/Resources/Views/Shared/NotFound.zh-CN.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 不要试图寻找这个页面。这是不可能的。相反...只需要领悟真相: + + + 根本没有页面! + + \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Views/Auth/AccessDenied.cshtml b/src/Lumina.Presentation.Web/Views/Auth/AccessDenied.cshtml index 07ff0ba5..cfec2b7a 100644 --- a/src/Lumina.Presentation.Web/Views/Auth/AccessDenied.cshtml +++ b/src/Lumina.Presentation.Web/Views/Auth/AccessDenied.cshtml @@ -11,7 +11,7 @@ - @_localizer["NotAllowedToAccessResource"] + @_localizer["NotAllowedToAccessResource"] diff --git a/src/Lumina.Presentation.Web/Views/Shared/NotFound.cshtml b/src/Lumina.Presentation.Web/Views/Shared/NotFound.cshtml new file mode 100644 index 00000000..cd38a425 --- /dev/null +++ b/src/Lumina.Presentation.Web/Views/Shared/NotFound.cshtml @@ -0,0 +1,19 @@ +@* +/not-found/ +*@ + +@using Microsoft.AspNetCore.Mvc.Localization + +@inject IViewLocalizer _localizer + + + + + + + + @_localizer["PageNotFound"] + @_localizer["ThereIsNoPage"] + + +
@_localizer["NotAllowedToAccessResource"]
@_localizer["PageNotFound"]
@_localizer["ThereIsNoPage"]