為什么異常處理選擇中間件?
傳統的ASP.NET可以采用異常過濾器的方式處理異常,在ASP.NET CORE中,是以多個中間件連接而成的管道形式處理請求的,不過常用的五大過濾器得以保留,同樣可以采用異常過濾器處理異常,但是異常過濾器不能處理MVC中間件以外的異常,為了全局統一考慮,采用中間件處理異常更為合適
為什么選擇自定義異常中間件?
先來看看ASP.NET CORE 內置的三個異常處理中間件 DeveloperExceptionPageMiddleware, ExceptionHandlerMiddleware,StatusCodePagesMiddleware
1.DeveloperExceptionPageMiddleware
能給出詳細的請求/返回/錯誤信息,因為包含敏感信息,所以僅適合開發環境
2.ExceptionHandlerMiddleware (蔣神博客:http://www.ythuaji.com.cn/article/73253.html)
僅處理500錯誤
3.StatusCodePagesMiddleware (蔣神博客:http://www.ythuaji.com.cn/article/73252.html)
能處理400-599之間的錯誤,但需要Response中不能包含內容(ContentLength=0 && ContentType=null,經實驗不能響應mvc里未捕獲異常)
由于ExceptionHandlerMiddleware和StatusCodePagesMiddleware的各自的限制條件,兩者需要搭配使用。相比之下自定義中間件更加靈活,既能對各種錯誤狀態進行統一處理,也能按照配置決定處理方式。
CustomExceptionMiddleWare
首先聲明異常中間件的配置類
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
30
31
32
33
34
35
36
37
38
39
40
41
|
/// <summary> /// 異常中間件配置對象 /// </summary> public class CustomExceptionMiddleWareOption { public CustomExceptionMiddleWareOption( CustomExceptionHandleType handleType = CustomExceptionHandleType.JsonHandle, IList<PathString> jsonHandleUrlKeys = null , string errorHandingPath = "" ) { HandleType = handleType; JsonHandleUrlKeys = jsonHandleUrlKeys; ErrorHandingPath = errorHandingPath; } /// <summary> /// 異常處理方式 /// </summary> public CustomExceptionHandleType HandleType { get ; set ; } /// <summary> /// Json處理方式的Url關鍵字 /// <para>僅HandleType=Both時生效</para> /// </summary> public IList<PathString> JsonHandleUrlKeys { get ; set ; } /// <summary> /// 錯誤跳轉頁面 /// </summary> public PathString ErrorHandingPath { get ; set ; } } /// <summary> /// 錯誤處理方式 /// </summary> public enum CustomExceptionHandleType { JsonHandle = 0, //Json形式處理 PageHandle = 1, //跳轉網頁處理 Both = 2 //根據Url關鍵字自動處理 } |
聲明異常中間件的成員
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
|
/// <summary> /// 管道請求委托 /// </summary> private RequestDelegate _next; /// <summary> /// 配置對象 /// </summary> private CustomExceptionMiddleWareOption _option; /// <summary> /// 需要處理的狀態碼字典 /// </summary> private IDictionary< int , string > exceptionStatusCodeDic; public CustomExceptionMiddleWare(RequestDelegate next, CustomExceptionMiddleWareOption option) { _next = next; _option = option; exceptionStatusCodeDic = new Dictionary< int , string > { { 401, "未授權的請求" }, { 404, "找不到該頁面" }, { 403, "訪問被拒絕" }, { 500, "服務器發生意外的錯誤" } //其余狀態自行擴展 }; } |
異常中間件主要邏輯
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
public async Task Invoke(HttpContext context) { Exception exception = null ; try { await _next(context); //調用管道執行下一個中間件 } catch (Exception ex) { context.Response.Clear(); context.Response.StatusCode = 500; //發生未捕獲的異常,手動設置狀態碼 exception = ex; } finally { if (exceptionStatusCodeDic.ContainsKey(context.Response.StatusCode) && !context.Items.ContainsKey( "ExceptionHandled" )) //預處理標記 { var errorMsg = string .Empty; if (context.Response.StatusCode == 500 && exception != null ) { errorMsg = $ "{exceptionStatusCodeDic[context.Response.StatusCode]}\r\n{(exception.InnerException != null ? exception.InnerException.Message : exception.Message)}" ; } else { errorMsg = exceptionStatusCodeDic[context.Response.StatusCode]; } exception = new Exception(errorMsg); } if (exception != null ) { var handleType = _option.HandleType; if (handleType == CustomExceptionHandleType.Both) //根據Url關鍵字決定異常處理方式 { var requestPath = context.Request.Path; handleType = _option.JsonHandleUrlKeys != null && _option.JsonHandleUrlKeys.Count( k => context.Request.Path.StartsWithSegments(k, StringComparison.CurrentCultureIgnoreCase)) > 0 ? CustomExceptionHandleType.JsonHandle : CustomExceptionHandleType.PageHandle; } if (handleType == CustomExceptionHandleType.JsonHandle) await JsonHandle(context, exception); else await PageHandle(context, exception, _option.ErrorHandingPath); } } } /// <summary> /// 統一格式響應類 /// </summary> /// <param name="ex"></param> /// <returns></returns> private ApiResponse GetApiResponse(Exception ex) { return new ApiResponse() { IsSuccess = false , Message = ex.Message }; } /// <summary> /// 處理方式:返回Json格式 /// </summary> /// <param name="context"></param> /// <param name="ex"></param> /// <returns></returns> private async Task JsonHandle(HttpContext context, Exception ex) { var apiResponse = GetApiResponse(ex); var serialzeStr = JsonConvert.SerializeObject(apiResponse); context.Response.ContentType = "application/json" ; await context.Response.WriteAsync(serialzeStr, Encoding.UTF8); } /// <summary> /// 處理方式:跳轉網頁 /// </summary> /// <param name="context"></param> /// <param name="ex"></param> /// <param name="path"></param> /// <returns></returns> private async Task PageHandle(HttpContext context, Exception ex, PathString path) { context.Items.Add( "Exception" , ex); var originPath = context.Request.Path; context.Request.Path = path; //設置請求頁面為錯誤跳轉頁面 try { await _next(context); } catch { } finally { context.Request.Path = originPath; //恢復原始請求頁面 } } |
使用擴展類進行中間件注冊
1
2
3
4
5
6
7
8
|
public static class CustomExceptionMiddleWareExtensions { public static IApplicationBuilder UseCustomException( this IApplicationBuilder app, CustomExceptionMiddleWareOption option) { return app.UseMiddleware<CustomExceptionMiddleWare>(option); } } |
在Startup.cs的Configuref方法中注冊異常中間件
1
2
3
4
|
app.UseCustomException( new CustomExceptionMiddleWareOption( handleType: CustomExceptionHandleType.Both, //根據url關鍵字決定處理方式 jsonHandleUrlKeys: new PathString[] { "/api" }, errorHandingPath: "/home/error" )); |
接下來我們來進行測試,首先模擬一個將會進行頁面跳轉的未經捕獲的異常
訪問/home/about的結果
訪問/home/test的結果 (該地址不存在)
OK異常跳轉頁面的方式測試完成,接下來我們測試返回統一格式(json)的異常處理,同樣先模擬一個未經捕獲的異常
訪問/api/token/gettesterror的結果
訪問/api/token/test的結果 (該地址不存在)
訪問/api/token/getvalue的結果 (該接口需要身份驗證)
測試完成,頁面跳轉和統一格式返回都沒有問題,自定義異常中間件已按預期工作
需要注意的是,自定義中間件會響應每個HTTP請求,所以處理邏輯一定要精簡,防止發生不必要的性能問題
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://www.cnblogs.com/ShenNan/p/10197231.html