Skip to content

Commit 423d138

Browse files
update Error handling doc for 5.0 (#19741)
* update Error handling doc for 5.0 * work * work * work * work * work * work * work * work * Apply suggestions from code review Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com> Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com>
1 parent b288444 commit 423d138

48 files changed

Lines changed: 40977 additions & 40 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

aspnetcore/fundamentals/error-handling.md

Lines changed: 270 additions & 31 deletions
Large diffs are not rendered by default.

aspnetcore/fundamentals/error-handling/samples/2.x/ErrorHandlingSample/ErrorHandlingSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.App"/>
10+
<PackageReference Include="Microsoft.AspNetCore.App" />
1111
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
1212
</ItemGroup>
1313

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@page
2+
@model ErrorModel
3+
@{
4+
ViewData["Title"] = "Error";
5+
}
6+
7+
<h1 class="text-danger">Error.</h1>
8+
<h2 class="text-danger">An error occurred while processing your request.</h2>
9+
10+
11+
12+
<h3>Development Mode</h3>
13+
<p>
14+
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
15+
</p>
16+
<p>
17+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
18+
It can result in displaying sensitive information from exceptions to end users.
19+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
20+
and restarting the app.
21+
</p>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Net;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Diagnostics;
9+
using Microsoft.AspNetCore.Mvc;
10+
using Microsoft.AspNetCore.Mvc.RazorPages;
11+
12+
namespace ErrorHandlingSample.Pages.MyFolder
13+
{
14+
#region snippet_ExceptionHandlerPathFeature
15+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
16+
public class ErrorModel : PageModel
17+
{
18+
public string RequestId { get; set; }
19+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
20+
public string ExceptionMessage { get; set; }
21+
22+
public void OnGet()
23+
{
24+
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
25+
26+
var exceptionHandlerPathFeature =
27+
HttpContext.Features.Get<IExceptionHandlerPathFeature>();
28+
if (exceptionHandlerPathFeature?.Error is FileNotFoundException)
29+
{
30+
ExceptionMessage = "File error thrown";
31+
}
32+
if (exceptionHandlerPathFeature?.Path == "/index")
33+
{
34+
ExceptionMessage += " from home page";
35+
}
36+
}
37+
}
38+
#endregion
39+
}

aspnetcore/fundamentals/error-handling/samples/2.x/ErrorHandlingSample/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

aspnetcore/fundamentals/error-handling/samples/2.x/ErrorHandlingSample/Startup.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// The ErrorHandler directives must be used along with the ProdEnvironment directive.
1515
// The DeveloperExceptionPage is seen only when the DevEnvironment directive is used.
1616

17-
#define StatusCodePages // or StatusCodePagesWithLambda or // StatusCodePagesWithFormatString or StatusCodePagesWithRedirect or StatusCodePagesWithReExecute
17+
#define StatusCodePagesWithRedirect // StatusCodePages // or StatusCodePagesWithLambda or // StatusCodePagesWithFormatString or StatusCodePagesWithRedirect or StatusCodePagesWithReExecute
1818
#define ErrorHandlerPage // or ErrorHandlerLambda
1919
#define ProdEnvironment // or DevEnvironment
2020

@@ -99,10 +99,6 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
9999
var exceptionHandlerPathFeature =
100100
context.Features.Get<IExceptionHandlerPathFeature>();
101101

102-
// Use exceptionHandlerPathFeature to process the exception (for example,
103-
// logging), but do NOT expose sensitive error information directly to
104-
// the client.
105-
106102
if (exceptionHandlerPathFeature?.Error is FileNotFoundException)
107103
{
108104
await context.Response.WriteAsync("File error thrown!<br><br>\r\n");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@page
2+
@{
3+
Layout = null; // clean up F12 tool network tab
4+
}
5+
@model ErrorModel
6+
@{
7+
ViewData["Title"] = "Error";
8+
}
9+
<head> <!-- prevent favicon.ico from being requested. -->
10+
<link rel="icon" href="data:,">
11+
</head>
12+
<h1 class="text-danger">Error from Error Page.</h1>
13+
<h2 class="text-danger">An error occurred while processing your request.</h2>
14+
15+
@if (Model.ShowRequestId)
16+
{
17+
<p>
18+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
19+
</p>
20+
}
21+
22+
<h3>Exception Message</h3>
23+
<p>
24+
@Model.ExceptionMessage
25+
</p>
26+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Microsoft.AspNetCore.Diagnostics;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.RazorPages;
4+
using Microsoft.Extensions.Logging;
5+
using System.Diagnostics;
6+
using System.IO;
7+
8+
namespace ErrorHandlingSample.Pages
9+
{
10+
#region snippet
11+
[ResponseCache(Duration=0, Location=ResponseCacheLocation.None, NoStore=true)]
12+
[IgnoreAntiforgeryToken]
13+
public class ErrorModel : PageModel
14+
{
15+
public string RequestId { get; set; }
16+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
17+
public string ExceptionMessage { get; set; }
18+
private readonly ILogger<ErrorModel> _logger;
19+
20+
public ErrorModel(ILogger<ErrorModel> logger)
21+
{
22+
_logger = logger;
23+
}
24+
25+
public void OnGet()
26+
{
27+
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
28+
29+
var exceptionHandlerPathFeature =
30+
HttpContext.Features.Get<IExceptionHandlerPathFeature>();
31+
if (exceptionHandlerPathFeature?.Error is FileNotFoundException)
32+
{
33+
ExceptionMessage = "File error thrown";
34+
_logger.LogError(ExceptionMessage);
35+
}
36+
if (exceptionHandlerPathFeature?.Path == "/index")
37+
{
38+
ExceptionMessage += " from home page";
39+
}
40+
}
41+
}
42+
#endregion
43+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@page "{code:int?}"
2+
@model IndexModel
3+
@{
4+
ViewData["Title"] = "Home page";
5+
}
6+
7+
<div class="text-left">
8+
<p>
9+
<a href="/NoSuchPage">
10+
Request an endpoint that doesn't exist. Trigger a 404
11+
</a>.
12+
</p>
13+
<p><a href="/index/1">Trigger an exceptionn</a>.</p>
14+
<p><a href="/index/2">Return a 500 error.</a>.</p>
15+
</div>

0 commit comments

Comments
 (0)