diff --git a/Apps/Website/Controllers/HomeController.cs b/Apps/Website/Controllers/HomeController.cs index 88a2211..dbcfdca 100644 --- a/Apps/Website/Controllers/HomeController.cs +++ b/Apps/Website/Controllers/HomeController.cs @@ -24,6 +24,12 @@ namespace Website.Controllers return View(); } + [HttpGet("Status/404")] + public IActionResult PageNotFound() + { + return View(); + } + public IActionResult Privacy() { return View(); diff --git a/Apps/Website/Controllers/LinksController.cs b/Apps/Website/Controllers/LinksController.cs index ff29da8..e18a9f6 100644 --- a/Apps/Website/Controllers/LinksController.cs +++ b/Apps/Website/Controllers/LinksController.cs @@ -23,6 +23,8 @@ namespace Website } [HttpGet("{controller}/detail/{linkId:guid}")] + // [TypeFilter(typeof(CustomExceptionFilter))] // Permet d'appliquer un filtre ASP.Net + // sur une action uniquement (valable pour toutes les actions d'un controlleur si appliqué sur ce dernier) public IActionResult Show(Guid linkId) { // ViewData["Comments"] = _commentService.GetAllLinkComments(linkId); diff --git a/Apps/Website/CustomExceptionFilter.cs b/Apps/Website/CustomExceptionFilter.cs new file mode 100644 index 0000000..d7d6c8a --- /dev/null +++ b/Apps/Website/CustomExceptionFilter.cs @@ -0,0 +1,18 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; + +namespace Website +{ + public class CustomExceptionFilter : IExceptionFilter + { + public void OnException(ExceptionContext context) + { + // FIXME en théorie, on aurait levé une exception personnalisée beaucoup + // moins globale pour éviter de catcher des choses non pertinentes. + if (context.Exception is System.InvalidOperationException) + { + context.Result = new NotFoundResult(); + } + } + } +} \ No newline at end of file diff --git a/Apps/Website/Startup.cs b/Apps/Website/Startup.cs index 19476a6..0598c52 100644 --- a/Apps/Website/Startup.cs +++ b/Apps/Website/Startup.cs @@ -41,7 +41,11 @@ namespace Website }); services.AddTransient(); services.AddTransient(); - services.AddControllersWithViews(); + + services.AddControllersWithViews(options => + { + options.Filters.Add(); + }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -70,6 +74,7 @@ namespace Website app.UseHsts(); } app.UseHttpsRedirection(); + app.UseStatusCodePagesWithRedirects("/Status/{0}"); app.UseStaticFiles(); app.UseRouting(); diff --git a/Apps/Website/Views/Home/PageNotFound.cshtml b/Apps/Website/Views/Home/PageNotFound.cshtml new file mode 100644 index 0000000..a5f920d --- /dev/null +++ b/Apps/Website/Views/Home/PageNotFound.cshtml @@ -0,0 +1,5 @@ +@{ + ViewData["Title"] = "Page not found"; +} + +

Oh noes

\ No newline at end of file