From 647696aa92f4f1b53cfb65b7332a084d4b397830 Mon Sep 17 00:00:00 2001 From: Robert Marshall Date: Sat, 27 Apr 2019 22:03:50 +0100 Subject: [PATCH] Refactor error handling. Add 404 page --- Website/Controllers/ErrorController.cs | 27 +++++++++++++++++++ Website/Controllers/HomeController.cs | 11 +------- Website/Startup.cs | 10 +++---- Website/Views/Error/PageNotFound.cshtml | 7 +++++ .../Error.cshtml => Error/ServerError.cshtml} | 0 5 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 Website/Controllers/ErrorController.cs create mode 100644 Website/Views/Error/PageNotFound.cshtml rename Website/Views/{Shared/Error.cshtml => Error/ServerError.cshtml} (100%) diff --git a/Website/Controllers/ErrorController.cs b/Website/Controllers/ErrorController.cs new file mode 100644 index 0000000..f553e03 --- /dev/null +++ b/Website/Controllers/ErrorController.cs @@ -0,0 +1,27 @@ +using System; +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc; +using Website.ViewModels; + +namespace Website.Controllers +{ + public class ErrorController : Controller + { + + public IActionResult BlowUp() { + throw new Exception(); + } + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public IActionResult ServerError() + { + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); + } + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public IActionResult PageNotFound() + { + return View(); + } + } +} diff --git a/Website/Controllers/HomeController.cs b/Website/Controllers/HomeController.cs index 0e46dab..4e3a5a4 100644 --- a/Website/Controllers/HomeController.cs +++ b/Website/Controllers/HomeController.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; +using System.Diagnostics; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Website.Data; @@ -23,11 +20,5 @@ namespace Website.Controllers var post = await _blogRepo.GetLatestPostAsync(); return View(post); } - - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] - public IActionResult Error() - { - return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); - } } } diff --git a/Website/Startup.cs b/Website/Startup.cs index a5fa2f0..756dcdf 100644 --- a/Website/Startup.cs +++ b/Website/Startup.cs @@ -1,11 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -51,7 +46,8 @@ namespace Website } else { - app.UseExceptionHandler("/Home/Error"); + app.UseExceptionHandler("/Error/ServerError"); + app.UseStatusCodePagesWithReExecute("/Error/PageNotFound"); // 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/Website/Views/Error/PageNotFound.cshtml b/Website/Views/Error/PageNotFound.cshtml new file mode 100644 index 0000000..fbbfb09 --- /dev/null +++ b/Website/Views/Error/PageNotFound.cshtml @@ -0,0 +1,7 @@ +@model ErrorViewModel +@{ + ViewData["Title"] = "404 Not Found"; +} + +

404 Not Found - page unavailable.

+This page cannot be found. \ No newline at end of file diff --git a/Website/Views/Shared/Error.cshtml b/Website/Views/Error/ServerError.cshtml similarity index 100% rename from Website/Views/Shared/Error.cshtml rename to Website/Views/Error/ServerError.cshtml