在上面的示例中,我們的 action 內部有一個 try-catch
代碼塊。這一點很重要,我們需要在我們的 action 方法體中處理所有的異常(包括未處理的)。一些開發者在 action 中使用 try-catch
代碼塊,這種方式明顯沒有任何問題。但我們希望 action 盡量保持簡潔。因此,從我們的 action 中刪除 try-catch
,並將其放在一個集中的地方會是一種更好的方式。.NET Core 給我們提供了一種處理全局異常的方式,只需要稍加修改,就可以使用內置且完善的的中間件。我們需要做的修改就是在 Startup
類中修改 Configure
方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public
void
Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseExceptionHandler(config =>
{
config.Run(async context =>
{
context.Response.StatusCode = 500;
context.Response.ContentType =
"application/json"
;
var
error = context.Features.Get<IExceptionHandlerFeature>();
if
(error !=
null
)
{
var
ex = error.Error;
await context.Response.WriteAsync(
new
ErrorModel
{
StatusCode = 500,
ErrorMessage = ex.Message
}.ToString());
}
});
});
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
|
我們也可以通過創建自定義的中間件來實現我們的自定義異常處理:
// You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project public class CustomExceptionMiddleware { private readonly RequestDelegate _next; private readonly ILogger<CustomExceptionMiddleware> _logger; public CustomExceptionMiddleware(RequestDelegate next, ILogger<CustomExceptionMiddleware> logger) { _next = next; _logger = logger; } public async Task Invoke(HttpContext httpContext) { try { await _next(httpContext); } catch (Exception ex) { _logger.LogError("Unhandled exception....", ex); await HandleExceptionAsync(httpContext, ex); } } private Task HandleExceptionAsync(HttpContext httpContext, Exception ex) { //todo return Task.CompletedTask; } } // Extension method used to add the middleware to the HTTP request pipeline. public static class CustomExceptionMiddlewareExtensions { public static IApplicationBuilder UseCustomExceptionMiddleware(this IApplicationBuilder builder) { return builder.UseMiddleware<CustomExceptionMiddleware>(); } }
之后,我們只需要將其注入到應用程序的請求管道中即可:
1
2
3
4
|
public
void
Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCustomExceptionMiddleware();
}
|