Built-in exception page
public static class Sample02
{
public static void Run()
{
Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder
.ConfigureServices(services => services.AddRouting())
.Configure(app => app
.UseDeveloperExceptionPage()
.UseRouting()
.UseEndpoints(routeBuilder => routeBuilder.MapGet("/", HandleAsync))
))
.Build()
.Run();
}
private static Task HandleAsync(HttpContext context)
{
return Task.FromException(new InvalidOperationException("This is Exception")); }}Copy the code
Custom exception page
public static class Sample03
{
public static void Run()
{
var options = new ExceptionHandlerOptions
{
ExceptionHandler = context => context.Response.WriteAsync("Hello Exception!")}; Host.CreateDefaultBuilder() .ConfigureWebHostDefaults(builder => builder .Configure(app => app .UseExceptionHandler(options) .Run(context => Task.FromException(new InvalidOperationException("Throw Exception"))) )) .Build() .Run(); }}Copy the code
public static class Sample04
{
public static void Run()
{
Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder
.Configure(app => app
.UseExceptionHandler(applicationBuilder => applicationBuilder
.Run(context => context.Response.WriteAsync("Hello Exception!"))
)
.Run(context => Task.FromException(new InvalidOperationException("This is an anomaly"))) )) .Build() .Run(); }}Copy the code
public static class Sample05
{
public static void Run()
{
Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder
.ConfigureServices(services => services.AddRouting())
.Configure(app => app
.UseExceptionHandler("/error")
.UseRouting()
.UseEndpoints(routeBuilder => routeBuilder.MapGet("error", HandleAsync))
.Run(context => Task.FromException(new InvalidOperationException("Throw Exception")))
))
.Build()
.Run();
}
private static Task HandleAsync(HttpContext context)
{
return context.Response.WriteAsync("Hello Exception"); }}Copy the code
Status code error page
public static class Sample06
{
public static void Run()
{
Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder
.Configure(app => app
.UseStatusCodePages("text/plain"."Error ({0})")
.Run(context => Task.Run(() => context.Response.StatusCode = 500)) )) .Build() .Run(); }}Copy the code
public static class Sample07
{
private static readonly Random Random = new Random();
public static void Run()
{
Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder
.Configure(app => app
.UseStatusCodePages(Handler)
.Run(context => Task.Run(() => context.Response.StatusCode = Random.Next(400.599)))
))
.Build()
.Run();
}
private static async Task Handler(StatusCodeContext context)
{
var response = context.HttpContext.Response;
if (response.StatusCode < 500)
{
await response.WriteAsync($"Client Error ({response.StatusCode})");
}
else
{
await response.WriteAsync($"Server Error ({response.StatusCode})"); }}}Copy the code
public static class Sample08
{
private static readonly Random Random = new Random();
public static void Run()
{
Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder
.ConfigureServices(collection => collection.AddRouting())
.Configure(app => app
.UseStatusCodePagesWithRedirects("/error/{0}")
.UseRouting()
.UseEndpoints(routeBuilder => routeBuilder.MapGet("error/{status_code}", HandlerError))
.Run(context => Task.Run(() => context.Response.StatusCode = Random.Next(400.599)))
))
.Build()
.Run();
}
private static async Task HandlerError(HttpContext context)
{
var code = context.GetRouteData().Values["status_code"];
await context.Response.WriteAsync($"Error ({code})"); }}Copy the code
public static class Sample09
{
private static readonly Random Random = new Random();
public static void Run()
{
Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder
.ConfigureServices(collection => collection.AddRouting())
.Configure(app => app
.UseStatusCodePagesWithReExecute("/error/{0}")
.UseRouting()
.UseEndpoints(routeBuilder => routeBuilder.MapGet("error/{status_code}", HandlerError))
.Run(context => Task.Run(() => context.Response.StatusCode = Random.Next(400.599)))
))
.Build()
.Run();
}
private static async Task HandlerError(HttpContext context)
{
var code = context.GetRouteData().Values["status_code"];
await context.Response.WriteAsync($"Error ({code})"); }}Copy the code
In practice, exception Settings are used
Install the following packages first
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
Copy the code
class Program
{
static void Main(string[] args)
{
var options = new DeveloperExceptionPageOptions
{
SourceCodeLineCount = 3}; Host.CreateDefaultBuilder() .ConfigureWebHostDefaults(builder => builder .ConfigureServices(collection => collection .AddRouting() .AddControllersWithViews() .AddRazorRuntimeCompilation()) .Configure(app=>app .UseDeveloperExceptionPage(options) .UseRouting() .UseEndpoints(routeBuilder => routeBuilder.MapControllers())) ) .Build() .Run(); }}public class HomeController : Controller{[HttpGet("/")]
public IActionResult Index() => View();
}
Copy the code