Skip to content

Commit 90c0b46

Browse files
committed
Add IExceptionHandler sample
1 parent dff2116 commit 90c0b46

File tree

6 files changed

+83
-13
lines changed

6 files changed

+83
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Samples for ASP.NET Core 6.0, 7.0 and 8.0 RC 1 (499)
1+
# Samples for ASP.NET Core 6.0, 7.0 and 8.0 RC 1 (500)
22

3-
- Samples for ASP.NET Core **8.0 RC 1** is available [here](/projects/.net8) (36).
3+
- Samples for ASP.NET Core **8.0 RC 1** is available [here](/projects/.net8) (37).
44
- Samples for ASP.NET Core **7.0** is available [here](/projects/.net7) (45).
55
- Samples for ASP.NET Core **8.0 Preview 6** using EdgeDB.NET is [here](https://github.com/edgedb/edgedb-net).
66

projects/.net8/README.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ASP.NET 8.0 Preview 7 (36)
1+
# ASP.NET 8.0 Preview 7 (37)
22

33
These samples require [.NET 8.0 RC 1](https://github.com/dotnet/installer#table).
44

@@ -129,26 +129,31 @@ These samples require [.NET 8.0 RC 1](https://github.com/dotnet/installer#table)
129129

130130
## Request Timeout
131131

132-
* [Request Timeout](request-timeout)
132+
* [Request Timeout](request-timeout)
133133

134-
This sample demonstrates how to configure a request timeout in Minimal API.
134+
This sample demonstrates how to configure a request timeout in Minimal API.
135135

136-
* [Request Timeout Policy](request-timeout-2)
136+
* [Request Timeout Policy](request-timeout-2)
137137

138-
Trigger exception on a timeout using `HttpContext.RequestAborted.ThrowIfCancellationRequested()` on a timeout that was specified using a named policy in Minimal API.
138+
Trigger exception on a timeout using `HttpContext.RequestAborted.ThrowIfCancellationRequested()` on a timeout that was specified using a named policy in Minimal API.
139139

140-
* [Request Timeout Policy](request-timeout-3)
140+
* [Request Timeout Policy](request-timeout-3)
141141

142-
Trigger exception on a timeout using `HttpContext.RequestAborted.ThrowIfCancellationRequested()` on a default timeout policy in Minimal API.
142+
Trigger exception on a timeout using `HttpContext.RequestAborted.ThrowIfCancellationRequested()` on a default timeout policy in Minimal API.
143143

144-
* [Request Timeout](request-timeout-4)
144+
* [Request Timeout](request-timeout-4)
145145

146-
Use `RequestTimeout` attribute in an MVC controller to specify when timeout is trigerred.
146+
Use `RequestTimeout` attribute in an MVC controller to specify when timeout is trigerred.
147147

148-
* [Request Timeout](request-timeout-5)
148+
* [Request Timeout](request-timeout-5)
149149

150-
Use `RequestTimeout` attribute in an MVC controller to use a named policy.
150+
Use `RequestTimeout` attribute in an MVC controller to use a named policy.
151151

152+
## IExceptionHandler
153+
154+
* [IExceptionHandler](iexception-handler)
155+
156+
Implement `IExceptionHandler` to handle ASP.NET Core exceptions.
152157

153158
## Keyed Services
154159

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.AspNetCore.Diagnostics;
2+
3+
var builder = WebApplication.CreateBuilder();
4+
builder.Services.AddExceptionHandler<DefaultExceptionHandler>();
5+
6+
var app = builder.Build();
7+
app.UseExceptionHandler(opt => { });
8+
9+
app.MapGet("/", () =>
10+
{
11+
throw new Exception("Something went wrong");
12+
});
13+
14+
app.Run();
15+
16+
public class DefaultExceptionHandler : IExceptionHandler
17+
{
18+
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
19+
{
20+
await httpContext.Response.WriteAsync($"""
21+
<html>
22+
<body>
23+
Opps sorry. Exception message is {exception.Message} at {httpContext.Request.Method} {httpContext.Request.Path}.
24+
</body>
25+
</html>
26+
""");
27+
return true;
28+
}
29+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Implement IExceptionHandler to handle unhandled exceptions
2+
3+
Implement `IExceptionHandler` for handling exceptions.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<ImplicitUsings>true</ImplicitUsings>
5+
<LangVersion>preview</LangVersion>
6+
<PreviewFeatures>true</PreviewFeatures>
7+
</PropertyGroup>
8+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.002.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "iexception-handler", "iexception-handler.csproj", "{14D0E714-54E8-409E-A7F7-DAE9860E46AD}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{14D0E714-54E8-409E-A7F7-DAE9860E46AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{14D0E714-54E8-409E-A7F7-DAE9860E46AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{14D0E714-54E8-409E-A7F7-DAE9860E46AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{14D0E714-54E8-409E-A7F7-DAE9860E46AD}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {066322E2-9E78-45D8-BDD7-7679632BD24A}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)