欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET CORE學習教程之自定義異常處理詳解

 更新時間:2019年01月03日 08:35:40   作者:#山雞  
這篇文章主要給大家介紹了關于ASP.NET CORE學習教程之自定義異常處理的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

為什么異常處理選擇中間件?

傳統(tǒng)的ASP.NET可以采用異常過濾器的方式處理異常,在ASP.NET CORE中,是以多個中間件連接而成的管道形式處理請求的,不過常用的五大過濾器得以保留,同樣可以采用異常過濾器處理異常,但是異常過濾器不能處理MVC中間件以外的異常,為了全局統(tǒng)一考慮,采用中間件處理異常更為合適

為什么選擇自定義異常中間件?

先來看看ASP.NET CORE 內(nèi)置的三個異常處理中間件 DeveloperExceptionPageMiddleware, ExceptionHandlerMiddleware,StatusCodePagesMiddleware

1.DeveloperExceptionPageMiddleware

能給出詳細的請求/返回/錯誤信息,因為包含敏感信息,所以僅適合開發(fā)環(huán)境

2.ExceptionHandlerMiddleware (蔣神博客:http://www.dbjr.com.cn/article/153926.htm)

僅處理500錯誤

3.StatusCodePagesMiddleware (蔣神博客:http://www.dbjr.com.cn/article/153931.htm)

能處理400-599之間的錯誤,但需要Response中不能包含內(nèi)容(ContentLength=0 && ContentType=null,經(jīng)實驗不能響應mvc里未捕獲異常)

由于ExceptionHandlerMiddleware和StatusCodePagesMiddleware的各自的限制條件,兩者需要搭配使用。相比之下自定義中間件更加靈活,既能對各種錯誤狀態(tài)進行統(tǒng)一處理,也能按照配置決定處理方式。

CustomExceptionMiddleWare

首先聲明異常中間件的配置類

/// <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, //跳轉網(wǎng)頁處理
 Both = 2  //根據(jù)Url關鍵字自動處理
 }

聲明異常中間件的成員

/// <summary>
 /// 管道請求委托
 /// </summary>
 private RequestDelegate _next;

 /// <summary>
 /// 配置對象
 /// </summary>
 private CustomExceptionMiddleWareOption _option;

 /// <summary>
 /// 需要處理的狀態(tài)碼字典
 /// </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, "服務器發(fā)生意外的錯誤" }
  //其余狀態(tài)自行擴展
  };
 }

異常中間件主要邏輯

public async Task Invoke(HttpContext context)
 {
  Exception exception = null;
  try
  {
  await _next(context); //調(diào)用管道執(zhí)行下一個中間件
  }
  catch (Exception ex)
  {
  context.Response.Clear(); 
  context.Response.StatusCode = 500; //發(fā)生未捕獲的異常,手動設置狀態(tài)碼
  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) //根據(jù)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>
 /// 統(tǒng)一格式響應類
 /// </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>
 /// 處理方式:跳轉網(wǎng)頁
 /// </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; //恢復原始請求頁面
  }
 }

使用擴展類進行中間件注冊

public static class CustomExceptionMiddleWareExtensions
 {

 public static IApplicationBuilder UseCustomException(this IApplicationBuilder app, CustomExceptionMiddleWareOption option)
 {
  return app.UseMiddleware<CustomExceptionMiddleWare>(option);
 }
 }

在Startup.cs的Configuref方法中注冊異常中間件

 app.UseCustomException(new CustomExceptionMiddleWareOption(
   handleType: CustomExceptionHandleType.Both, //根據(jù)url關鍵字決定處理方式
   jsonHandleUrlKeys: new PathString[] { "/api" },
   errorHandingPath: "/home/error"));

接下來我們來進行測試,首先模擬一個將會進行頁面跳轉的未經(jīng)捕獲的異常

訪問/home/about的結果

訪問/home/test的結果 (該地址不存在)

OK異常跳轉頁面的方式測試完成,接下來我們測試返回統(tǒng)一格式(json)的異常處理,同樣先模擬一個未經(jīng)捕獲的異常

訪問/api/token/gettesterror的結果

訪問/api/token/test的結果 (該地址不存在)

訪問/api/token/getvalue的結果 (該接口需要身份驗證)

測試完成,頁面跳轉和統(tǒng)一格式返回都沒有問題,自定義異常中間件已按預期工作

需要注意的是,自定義中間件會響應每個HTTP請求,所以處理邏輯一定要精簡,防止發(fā)生不必要的性能問題

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

最新評論