diff --git a/OliverBooth/Pages/Error.cshtml b/OliverBooth/Pages/Error.cshtml deleted file mode 100644 index b7d6b09..0000000 --- a/OliverBooth/Pages/Error.cshtml +++ /dev/null @@ -1,34 +0,0 @@ -@page "/error/{code:int?}" -@model OliverBooth.Pages.ErrorModel -@{ - Layout = "_MinimalLayout"; - ViewData["Title"] = "Error"; -} - -

- @switch (Model.HttpStatusCode) - { - case 403: - 403 Forbidden - break; - - case 404: - 404 Page not found - break; - - case 500: - Internal server error - break; - - default: - Something went wrong - break; - } -

- -@if (Model.ShowRequestId) -{ -

- Request ID: @Model.RequestId -

-} \ No newline at end of file diff --git a/OliverBooth/Pages/Error.cshtml.cs b/OliverBooth/Pages/Error.cshtml.cs deleted file mode 100644 index 04f1916..0000000 --- a/OliverBooth/Pages/Error.cshtml.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System.Diagnostics; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; - -namespace OliverBooth.Pages; - -[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] -[IgnoreAntiforgeryToken] -public class ErrorModel : PageModel -{ - public string? RequestId { get; set; } - - public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); - - public int HttpStatusCode { get; private set; } - - public IActionResult OnGet(int? code = null) - { - HttpStatusCode = code ?? HttpContext.Response.StatusCode; - if (HttpStatusCode == 200) - { - return RedirectToPage("/Index"); - } - - RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; - return Page(); - } -} diff --git a/OliverBooth/Pages/Error/BadRequest.cshtml b/OliverBooth/Pages/Error/BadRequest.cshtml new file mode 100644 index 0000000..d1e3fbc --- /dev/null +++ b/OliverBooth/Pages/Error/BadRequest.cshtml @@ -0,0 +1,17 @@ +@page "/error/400" +
+
+
+
+

400 Bad Request

+
+
+

Received invalid request message. Check your request and try again.

+
+
+ +
+ +
+
+
\ No newline at end of file diff --git a/OliverBooth/Pages/Error/Forbidden.cshtml b/OliverBooth/Pages/Error/Forbidden.cshtml new file mode 100644 index 0000000..2fd5597 --- /dev/null +++ b/OliverBooth/Pages/Error/Forbidden.cshtml @@ -0,0 +1,15 @@ +@page "/error/403" +
+
+
+

403 Forbidden

+
+
+ +
+ +
+

Access to the requested page is forbidden.

+
+
+
\ No newline at end of file diff --git a/OliverBooth/Pages/Error/GatewayTimeout.cshtml b/OliverBooth/Pages/Error/GatewayTimeout.cshtml new file mode 100644 index 0000000..b44d160 --- /dev/null +++ b/OliverBooth/Pages/Error/GatewayTimeout.cshtml @@ -0,0 +1,17 @@ +@page "/error/504" +
+
+
+ +
+ +
+
+

504 Gateway Timeout

+
+
+

The server is slacking. Give it more coffee.

+
+
+
+
\ No newline at end of file diff --git a/OliverBooth/Pages/Error/Gone.cshtml b/OliverBooth/Pages/Error/Gone.cshtml new file mode 100644 index 0000000..dc5084a --- /dev/null +++ b/OliverBooth/Pages/Error/Gone.cshtml @@ -0,0 +1,17 @@ +@page "/error/400" +
+
+
+
+

410 Gone

+
+
+

The requested page has mysteriously disappeared.

+
+
+ +
+ +
+
+
\ No newline at end of file diff --git a/OliverBooth/Pages/Error/ImATeapot.cshtml b/OliverBooth/Pages/Error/ImATeapot.cshtml new file mode 100644 index 0000000..c53f758 --- /dev/null +++ b/OliverBooth/Pages/Error/ImATeapot.cshtml @@ -0,0 +1,17 @@ +@page "/error/418" +
+
+
+ +
+ +
+
+

418 I'm A Teapot

+
+
+

No coffee available. I am only capable of brewing tea.

+
+
+
+
\ No newline at end of file diff --git a/OliverBooth/Pages/Error/InternalServerError.cshtml b/OliverBooth/Pages/Error/InternalServerError.cshtml new file mode 100644 index 0000000..27b4605 --- /dev/null +++ b/OliverBooth/Pages/Error/InternalServerError.cshtml @@ -0,0 +1,17 @@ +@page "/error/500" +
+
+
+ +
+ +
+
+

500 Internal Server Error

+
+
+

This is my fault, not yours.

+
+
+
+
\ No newline at end of file diff --git a/OliverBooth/Pages/Error/NotFound.cshtml b/OliverBooth/Pages/Error/NotFound.cshtml new file mode 100644 index 0000000..cb54df4 --- /dev/null +++ b/OliverBooth/Pages/Error/NotFound.cshtml @@ -0,0 +1,17 @@ +@page "/error/404" +
+
+
+ +
+ +
+
+

404 Not Found

+
+
+

The requested page could not be found.

+
+
+
+
\ No newline at end of file diff --git a/OliverBooth/Pages/Error/ServiceUnavailable.cshtml b/OliverBooth/Pages/Error/ServiceUnavailable.cshtml new file mode 100644 index 0000000..a33a4de --- /dev/null +++ b/OliverBooth/Pages/Error/ServiceUnavailable.cshtml @@ -0,0 +1,17 @@ +@page "/error/503" +
+
+
+
+

503 Service Unavailable

+
+
+

The server is currently unable to process your request. Please try again later.

+
+
+ +
+ +
+
+
\ No newline at end of file diff --git a/OliverBooth/Pages/Error/TooManyRequests.cshtml b/OliverBooth/Pages/Error/TooManyRequests.cshtml new file mode 100644 index 0000000..046b860 --- /dev/null +++ b/OliverBooth/Pages/Error/TooManyRequests.cshtml @@ -0,0 +1,17 @@ +@page "/error/429" +
+
+
+
+

429 Too Many Requests

+
+
+

You are being rate limited.

+
+
+ +
+ +
+
+
\ No newline at end of file diff --git a/OliverBooth/Program.cs b/OliverBooth/Program.cs index 0da5115..2017340 100644 --- a/OliverBooth/Program.cs +++ b/OliverBooth/Program.cs @@ -81,9 +81,78 @@ if (builder.Environment.IsProduction()) WebApplication app = builder.Build(); +app.Use(async (ctx, next) => +{ + await next(); + + if (ctx.Response.HasStarted) + { + return; + } + + string? originalPath = ctx.Request.Path.Value; + ctx.Items["originalPath"] = originalPath; + + bool matchedErrorPage = false; + + switch (ctx.Response.StatusCode) + { + case 400: + ctx.Request.Path = "/error/401"; + matchedErrorPage = true; + break; + + case 403: + ctx.Request.Path = "/error/403"; + matchedErrorPage = true; + break; + + case 404: + ctx.Request.Path = "/error/404"; + matchedErrorPage = true; + break; + + case 410: + ctx.Request.Path = "/error/410"; + matchedErrorPage = true; + break; + + case 418: + ctx.Request.Path = "/error/418"; + matchedErrorPage = true; + break; + + case 429: + ctx.Request.Path = "/error/429"; + matchedErrorPage = true; + break; + + case 500: + ctx.Request.Path = "/error/500"; + matchedErrorPage = true; + break; + + case 503: + ctx.Request.Path = "/error/503"; + matchedErrorPage = true; + break; + + case 504: + ctx.Request.Path = "/error/504"; + matchedErrorPage = true; + break; + } + + if (matchedErrorPage) + { + await next(); + } +}); +app.UseStatusCodePagesWithReExecute("/error/{0}"); + if (!app.Environment.IsDevelopment()) { - app.UseExceptionHandler("/Error"); + app.UseExceptionHandler("/error/500"); // 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/img/error/400-bad-request.png b/src/img/error/400-bad-request.png new file mode 100644 index 0000000..4e09ca8 Binary files /dev/null and b/src/img/error/400-bad-request.png differ diff --git a/src/img/error/403-forbidden.png b/src/img/error/403-forbidden.png new file mode 100644 index 0000000..4abd030 Binary files /dev/null and b/src/img/error/403-forbidden.png differ diff --git a/src/img/error/404-not-found.png b/src/img/error/404-not-found.png new file mode 100644 index 0000000..803530d Binary files /dev/null and b/src/img/error/404-not-found.png differ diff --git a/src/img/error/410-gone.png b/src/img/error/410-gone.png new file mode 100644 index 0000000..69c32db Binary files /dev/null and b/src/img/error/410-gone.png differ diff --git a/src/img/error/418-im-a-teapot.png b/src/img/error/418-im-a-teapot.png new file mode 100644 index 0000000..82292e0 Binary files /dev/null and b/src/img/error/418-im-a-teapot.png differ diff --git a/src/img/error/429-too-many-requests.png b/src/img/error/429-too-many-requests.png new file mode 100644 index 0000000..89bf1fc Binary files /dev/null and b/src/img/error/429-too-many-requests.png differ diff --git a/src/img/error/500-internal-server-error.png b/src/img/error/500-internal-server-error.png new file mode 100644 index 0000000..04039fc Binary files /dev/null and b/src/img/error/500-internal-server-error.png differ diff --git a/src/img/error/503-service-unavailable.png b/src/img/error/503-service-unavailable.png new file mode 100644 index 0000000..6f2b738 Binary files /dev/null and b/src/img/error/503-service-unavailable.png differ diff --git a/src/img/error/504-gateway-timeout.png b/src/img/error/504-gateway-timeout.png new file mode 100644 index 0000000..6f48f98 Binary files /dev/null and b/src/img/error/504-gateway-timeout.png differ