-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use shape when rendering Http errors to allow cusomization from the UI (
- Loading branch information
1 parent
ed0ed91
commit 833a982
Showing
13 changed files
with
92 additions
and
24 deletions.
There are no files selected for viewing
29 changes: 9 additions & 20 deletions
29
src/OrchardCore.Modules/OrchardCore.Diagnostics/Controllers/DiagnosticsController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,16 @@ | ||
using System; | ||
using System.Net; | ||
using Microsoft.AspNetCore.Mvc; | ||
using OrchardCore.Diagnostics.ViewModels; | ||
|
||
namespace OrchardCore.Diagnostics.Controllers | ||
namespace OrchardCore.Diagnostics.Controllers; | ||
|
||
public class DiagnosticsController : Controller | ||
{ | ||
public class DiagnosticsController : Controller | ||
[IgnoreAntiforgeryToken] | ||
public IActionResult Error(int? status) | ||
{ | ||
[IgnoreAntiforgeryToken] | ||
public IActionResult Error(int? status) | ||
{ | ||
// Most commonly used error messages. | ||
ViewData["StatusCode"] = status; | ||
Enum.TryParse((status ?? 500).ToString(), true, out HttpStatusCode httpStatusCode); | ||
|
||
// Most commonly used error messages. | ||
ViewData["StatusCode"] = status; | ||
|
||
return httpStatusCode switch | ||
{ | ||
HttpStatusCode.Forbidden => View("Forbidden"), | ||
HttpStatusCode.NotFound => View("NotFound"), | ||
HttpStatusCode.BadRequest => View("BadRequest"), | ||
HttpStatusCode.Unauthorized => View("Unauthorized"), | ||
_ => View("Error"), | ||
}; | ||
} | ||
return View(new HttpErrorShapeViewModel(status)); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/OrchardCore.Modules/OrchardCore.Diagnostics/ViewModels/HttpErrorShapeViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Net; | ||
using OrchardCore.DisplayManagement.Views; | ||
|
||
namespace OrchardCore.Diagnostics.ViewModels; | ||
|
||
public class HttpErrorShapeViewModel : ShapeViewModel | ||
{ | ||
private const string ShapeType = "HttpError"; | ||
|
||
public int? Code { get; set; } | ||
|
||
public HttpStatusCode? HttpStatusCode { get; } | ||
|
||
public HttpErrorShapeViewModel(int? code) | ||
{ | ||
Code = code; | ||
|
||
// The type name is 'HttpError', which means any Http status code will be handled by the 'HttpError.cshtml' or 'HttpError.liquid'. | ||
Metadata.Type = ShapeType; | ||
|
||
if (code.HasValue && Enum.TryParse<HttpStatusCode>(Code.ToString(), out var httpStatusCode)) | ||
{ | ||
HttpStatusCode = httpStatusCode; | ||
|
||
// Assign an alternative for every status-code to enable the customization for each status. | ||
|
||
// (ex. 'HttpError-404.cshtml' or 'HttpError-404.liquid') | ||
Metadata.Alternates.Add($"{ShapeType}__{Code}"); | ||
|
||
// (ex. 'HttpError-NotFound.cshtml' or 'HttpError-NotFound.liquid') | ||
Metadata.Alternates.Add($"{ShapeType}__{httpStatusCode}"); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/OrchardCore.Modules/OrchardCore.Diagnostics/Views/Diagnostics/Error.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<h1>@T["An error occurred while executing this request."]</h1> | ||
@await DisplayAsync(Model) |
2 changes: 1 addition & 1 deletion
2
...stics/Views/Diagnostics/BadRequest.cshtml → ...nostics/Views/HttpError-BadRequest.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<h1>@T["Your browser sent a request that this server could not understand."]</h1> | ||
<h1>@T["Your browser sent a request that this server could not understand."]</h1> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
src/OrchardCore.Modules/OrchardCore.Diagnostics/Views/HttpError.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@model OrchardCore.Diagnostics.ViewModels.HttpErrorShapeViewModel | ||
|
||
<h1>@T["An error occurred while executing this request."]</h1> |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Diagnostics (`OrchardCore.Diagnostics`) | ||
|
||
## Purpose | ||
|
||
Enables you to present HTTP errors in a personalized style and offers a means to modify the response of particular HTML errors. By default, we utilizes the following templates. | ||
|
||
| Template | Description | | ||
| --------- | ----------- | | ||
| `HttpError__BadRequest` | Generates the `400` HTTP error page. You can adjust its appearance by modifying the `HttpError-BadRequest.cshtml` or `HttpError-BadRequest.liquid` views. | | ||
| `HttpError__Forbidden` | Generates the `403` HTTP error page. You can adjust its appearance by modifying the `HttpError-Forbidden.cshtml` or `HttpError-Forbidden.liquid` views. | | ||
| `HttpError__NotFound` | Generates the `404` HTTP error page. You can adjust its appearance by modifying the `HttpError-NotFound.cshtml` or `HttpError-NotFound.liquid` views. | | ||
| `HttpError__Unauthorized` | Generates the `401` HTTP error page. You can adjust its appearance by modifying the `HttpError-Unauthorized.cshtml` or `HttpError-Unauthorized.liquid` views. | | ||
| `HttpError__HttpStatusCode` | Fallback template which generates the HTTP error page when no explicit template defined (ex, `HttpError-MethodNotAllowed.cshtml`). You can adjust its appearance by modifying the `HttpError.cshtml` or `HttpError.liquid` views. | | ||
|
||
|
||
### Example | ||
|
||
To alter the presentation of a particular HTTP status code, you can easily create a template within your theme that corresponds to the HTTP status code. For instance, if you wish to customize the 404 error page, you can achieve this by creating a template in your theme named `HttpError-NotFound.cshtml` or `HttpError-NotFound.liquid` as outlined below. | ||
|
||
``` | ||
<h1>@T["The page could not be found."]</h1> | ||
``` | ||
|
||
We utilize a template named `HttpError` to structure the default output of the undefined error. | ||
|
||
## Utilizing the Templates Feature for Customizing Error Pages | ||
|
||
You can utilize the [Templates](../Templates/README.md) feature to modify the appearance of your error pages. | ||
|
||
To illustrate, if you want to alter the view of the `403 (Forbidden)` page using the Template feature, create a new Template named `HttpError__Forbidden` and insert your customized HTML as follows: | ||
|
||
``` | ||
<h2 class="text-danger">{{ "You do not have access permission to this page." | t }}</h2> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters