Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use shape when rendering Http errors to allow cusomization from the UI #14545

Merged
merged 12 commits into from
Oct 26, 2023
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
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"),
};
if (!status.HasValue || !Enum.TryParse<HttpStatusCode>(status.ToString(), out var httpStatusCode))
{
httpStatusCode = HttpStatusCode.InternalServerError;
}

return View(new HttpStatusCodeShapeViewModel(httpStatusCode));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Net;
using OrchardCore.DisplayManagement.Views;

namespace OrchardCore.Diagnostics.ViewModels;

public class HttpStatusCodeShapeViewModel : ShapeViewModel
{
public HttpStatusCode HttpStatusCode { get; }

public HttpStatusCodeShapeViewModel(HttpStatusCode statusCode)
{
HttpStatusCode = statusCode;
Metadata.Type = "HttpStatusCode";
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved

// The type name is 'HttpStatusCode', so any Http status code will be handled by the 'HttpStatusCode.cshtml' or 'HttpStatusCode.liquid'.
// However, assign an alternative for every 'StatusCode' to enable the customization for each Status (ex. 'NotFound.cshtml' or 'NotFound.liquid').
Metadata.Alternates.Add(statusCode.ToString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add the int value too as an alternate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the 404 to the alternate. We'll look at the library and see how we can use it widely but in a separate PR.

}
}
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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@model OrchardCore.Diagnostics.ViewModels.HttpStatusCodeShapeViewModel

<h1>@T["An error occurred while executing this request."]</h1>
1 change: 1 addition & 0 deletions src/docs/reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Here's a categorized overview of all built-in Orchard Core features at a glance.
- [Email](modules/Email/README.md)
- [Redis](modules/Redis/README.md)
- [Deployment](modules/Deployment/README.md)
- [Diagnostics](modules/Diagnostics/README.md)
- [Remote Deployment](modules/Deployment.Remote/README.md)
- [Sms](modules/Sms/README.md)

Expand Down
34 changes: 34 additions & 0 deletions src/docs/reference/modules/Diagnostics/README.md
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 |
| --------- | ----------- |
| `BadRequest` | Generates the `400` HTTP error page. You can adjust its appearance by modifying the `BadRequest.cshtml` or `BadRequest.liquid` views. |
| `Forbidden` | Generates the `403` HTTP error page. You can adjust its appearance by modifying the `Forbidden.cshtml` or `Forbidden.liquid` views. |
| `NotFound` | Generates the `404` HTTP error page. You can adjust its appearance by modifying the `NotFound.cshtml` or `NotFound.liquid` views. |
| `Unauthorized` | Generates the `401` HTTP error page. You can adjust its appearance by modifying the `Unauthorized.cshtml` or `Unauthorized.liquid` views. |
| `HttpStatusCode` | Fallback template which generates the HTTP error page when no explicit template defined (ex, `MethodNotAllowed.cshtml`). You can adjust its appearance by modifying the `HttpStatusCode.cshtml` or `HttpStatusCode.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 `NotFound.cshtml` or `NotFound.liquid` as outlined below.

```
<h1>@T["The page could not be found."]</h1>
```

We utilize a template named `HttpStatusCode` 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 `Forbidden` and insert your customized HTML as follows:

```
<h2 class="text-danger">{{ "You do not have access permission to this page." | t }}</h2>
```
13 changes: 13 additions & 0 deletions src/docs/releases/1.8.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ A new option for restarting a specific Workflow instance has been incorporated,

The `NavbarTop` zone is no longer used in the `TheAdmin` or `TheTheme` themes. Follow the Navbar details below on how to inject custom menu items in the navbar.

### Diagnostics Module

The HTTP error views are now in the form of shapes. If you have error views customization within your theme, you'll need to relocate them from `YourTheme/Views/OrchardCore.Diagnostics/Diagnostics` to `YourTheme/Views`.

Likewise, should you come across any of the following views within the `Shared` directory of your theme, relocate them directly to the `Views` folder.

- `BadRequest.cshtml` or `BadRequest.liquid`
- `Forbidden.cshtml` or `Forbidden.liquid`
- `NotFound.cshtml` or `NotFound.liquid`
- `Unauthorized.cshtml` or `Unauthorized.liquid`

Furthermore, in the event of customization, you'll need to rename the `Error.cshtml` view to `HttpStatusCode.cshtml`, as this serves as the default template for error pages.

## Change Logs

### TheTheme Theme
Expand Down