diff --git a/aspnetcore/fundamentals/error-handling.md b/aspnetcore/fundamentals/error-handling.md index 967ea803da4b..a40770077f2e 100644 --- a/aspnetcore/fundamentals/error-handling.md +++ b/aspnetcore/fundamentals/error-handling.md @@ -101,6 +101,53 @@ if (statusCodePagesFeature != null) } ``` +If using a `UseStatusCodePages*` overload that points to an endpoint within the app, create an MVC view or Razor Page for the endpoint. For example, the [dotnet new](/dotnet/core/tools/dotnet-new) template for a Razor Pages app produces the following page and page model class: + +*Error.cshtml*: + +```cshtml +@page +@model ErrorModel +@{ + ViewData["Title"] = "Error"; +} + +
+ Request ID: @Model.RequestId
+
+ Swapping to Development environment will display more detailed information about the error that occurred. +
++ Development environment should not be enabled in deployed applications, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the ASPNETCORE_ENVIRONMENT environment variable to Development, and restarting the application. +
+``` + +*Error.cshtml.cs*: + +```csharp +public class ErrorModel : PageModel +{ + public string RequestId { get; set; } + + public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public void OnGet() + { + RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; + } +} +``` + ## Exception-handling code Code in exception handling pages can throw exceptions. It's often a good idea for production error pages to consist of purely static content. @@ -127,7 +174,7 @@ When running on [IIS](/iis) or [IIS Express](/iis/extensions/introduction-to-iis Exception filters can be configured globally or on a per-controller or per-action basis in an MVC app. These filters handle any unhandled exception that occurs during the execution of a controller action or another filter, and are not called otherwise. Learn more about exception filters in [Filters](xref:mvc/controllers/filters). ->[!TIP] +> [!TIP] > Exception filters are good for trapping exceptions that occur within MVC actions, but they're not as flexible as error handling middleware. Prefer middleware for the general case, and use filters only where you need to do error handling *differently* based on which MVC action was chosen. ### Handling Model State Errors @@ -135,6 +182,3 @@ Exception filters can be configured globally or on a per-controller or per-actio [Model validation](xref:mvc/models/validation) occurs prior to invoking each controller action, and it's the action method's responsibility to inspect `ModelState.IsValid` and react appropriately. Some apps will choose to follow a standard convention for dealing with model validation errors, in which case a [filter](xref:mvc/controllers/filters) may be an appropriate place to implement such a policy. You should test how your actions behave with invalid model states. Learn more in [Test controller logic](xref:mvc/controllers/testing). - - -