Skip to content

Commit 844f678

Browse files
committed
Add error handling
Add Exception handling
1 parent 83fc37b commit 844f678

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "3.1.300"
3+
"version": "3.1.100"
44
}
55
}
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>netcoreapp3.1</TargetFramework>
4+
<AssemblyName>error-handling</AssemblyName>
5+
<OutputType>Exe</OutputType>
6+
<PackageId>error-handling</PackageId>
7+
</PropertyGroup>
8+
</Project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.AspNetCore.Diagnostics;
5+
using Microsoft.AspNetCore.Http;
6+
7+
namespace PracticalAspNetCore
8+
{
9+
public class Startup
10+
{
11+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
12+
{
13+
app.UseWelcomePage();
14+
15+
if (env.IsDevelopment())
16+
{
17+
app.UseDeveloperExceptionPage();
18+
}
19+
else
20+
{
21+
app.UseExceptionHandler(errorApp =>
22+
{
23+
errorApp.Run(async context =>
24+
{
25+
context.Response.StatusCode = 500;
26+
context.Response.ContentType = "text/html";
27+
28+
await context.Response.WriteAsync("<html lang=\"en\"><body>\r\n");
29+
await context.Response.WriteAsync("ERROR!<br><br>\r\n");
30+
31+
var exceptionHandlerPathFeature =
32+
context.Features.Get<IExceptionHandlerPathFeature>();
33+
34+
var exception = exceptionHandlerPathFeature?.Error;
35+
36+
await context.Response.WriteAsync("Exception Message:" + exception.Message);
37+
});
38+
});
39+
app.UseHsts();
40+
}
41+
}
42+
43+
}
44+
45+
public class Program
46+
{
47+
public static void Main(string[] args) =>
48+
CreateHostBuilder(args).Build().Run();
49+
50+
public static IHostBuilder CreateHostBuilder(string[] args) =>
51+
Host.CreateDefaultBuilder(args)
52+
.ConfigureWebHostDefaults(webBuilder =>
53+
webBuilder.UseStartup<Startup>());
54+
55+
}
56+
}

0 commit comments

Comments
 (0)