|
1 | 1 | @page "/Error" |
| 2 | +@page "/not-found" |
| 3 | +@layout PlaygroundLayout |
2 | 4 | @using System.Diagnostics |
| 5 | +@inject NavigationManager Navigation |
3 | 6 |
|
4 | | -<PageTitle>Error</PageTitle> |
| 7 | +<PageTitle>@ErrorTitle</PageTitle> |
5 | 8 |
|
6 | | -<h1 class="text-danger">Error.</h1> |
7 | | -<h2 class="text-danger">An error occurred while processing your request.</h2> |
| 9 | +<MudContainer MaxWidth="MaxWidth.Medium" Class="mt-8"> |
| 10 | + <MudPaper Elevation="3" Class="pa-8"> |
| 11 | + <div class="d-flex flex-column align-center"> |
| 12 | + <MudIcon Icon="@Icons.Material.Filled.ErrorOutline" Color="Color.Error" Style="font-size: 6rem;" Class="mb-4" /> |
8 | 13 |
|
9 | | -@if (ShowRequestId) |
10 | | -{ |
11 | | - <p> |
12 | | - <strong>Request ID:</strong> <code>@RequestId</code> |
13 | | - </p> |
14 | | -} |
| 14 | + <MudText Typo="Typo.h3" Color="Color.Error" Class="mb-2">@ErrorTitle</MudText> |
| 15 | + <MudText Typo="Typo.h6" Color="Color.Secondary" Class="mb-4">@ErrorMessage</MudText> |
| 16 | + |
| 17 | + @if (ShowRequestId) |
| 18 | + { |
| 19 | + <MudAlert Severity="Severity.Info" Class="mb-4"> |
| 20 | + <strong>Request ID:</strong> <code>@RequestId</code> |
| 21 | + </MudAlert> |
| 22 | + } |
15 | 23 |
|
16 | | -<h3>Development Mode</h3> |
17 | | -<p> |
18 | | - Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred. |
19 | | -</p> |
20 | | -<p> |
21 | | - <strong>The Development environment shouldn't be enabled for deployed applications.</strong> |
22 | | - It can result in displaying sensitive information from exceptions to end users. |
23 | | - For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> |
24 | | - and restarting the app. |
25 | | -</p> |
| 24 | + @if (StatusCode == 404) |
| 25 | + { |
| 26 | + <MudText Typo="Typo.body1" Class="mb-4 text-center"> |
| 27 | + The page you are looking for might have been removed, had its name changed, or is temporarily unavailable. |
| 28 | + </MudText> |
| 29 | + <MudButton Variant="Variant.Filled" Color="Color.Primary" Href="/" StartIcon="@Icons.Material.Filled.Home"> |
| 30 | + Go to Home |
| 31 | + </MudButton> |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + <MudText Typo="Typo.h6" Class="mt-4 mb-2">Development Mode</MudText> |
| 36 | + <MudText Typo="Typo.body2" Class="mb-2"> |
| 37 | + Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred. |
| 38 | + </MudText> |
| 39 | + <MudAlert Severity="Severity.Warning" Class="mt-4"> |
| 40 | + <strong>The Development environment shouldn't be enabled for deployed applications.</strong> |
| 41 | + It can result in displaying sensitive information from exceptions to end users. |
| 42 | + For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> |
| 43 | + and restarting the app. |
| 44 | + </MudAlert> |
| 45 | + } |
| 46 | + </div> |
| 47 | + </MudPaper> |
| 48 | +</MudContainer> |
26 | 49 |
|
27 | 50 | @code{ |
28 | 51 | [CascadingParameter] |
29 | 52 | private HttpContext? HttpContext { get; set; } |
30 | 53 |
|
| 54 | + [Parameter] |
| 55 | + public int? StatusCodeParameter { get; set; } |
| 56 | + |
31 | 57 | private string? RequestId { get; set; } |
32 | 58 | private bool ShowRequestId => !string.IsNullOrEmpty(RequestId); |
| 59 | + private int? StatusCode { get; set; } |
33 | 60 |
|
34 | | - protected override void OnInitialized() => |
| 61 | + private string ErrorTitle => StatusCode switch |
| 62 | + { |
| 63 | + 404 => "404 - Page Not Found", |
| 64 | + _ => "Error" |
| 65 | + }; |
| 66 | + |
| 67 | + private string ErrorMessage => StatusCode switch |
| 68 | + { |
| 69 | + 404 => "The page you requested could not be found.", |
| 70 | + _ => "An error occurred while processing your request." |
| 71 | + }; |
| 72 | + |
| 73 | + protected override void OnInitialized() |
| 74 | + { |
35 | 75 | RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier; |
| 76 | + |
| 77 | + // First check if status code was passed as a parameter |
| 78 | + if (StatusCodeParameter.HasValue) |
| 79 | + { |
| 80 | + StatusCode = StatusCodeParameter; |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + // Check for status code in query parameters |
| 85 | + var uri = new Uri(Navigation.Uri); |
| 86 | + var query = System.Web.HttpUtility.ParseQueryString(uri.Query); |
| 87 | + var statusParam = query["status"]; |
| 88 | + if (!string.IsNullOrEmpty(statusParam) && int.TryParse(statusParam, out var status)) |
| 89 | + { |
| 90 | + StatusCode = status; |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + // Default to 404 if no status code is provided (e.g., when used as NotFoundPage) |
| 95 | + StatusCode = 404; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
36 | 99 | } |
0 commit comments