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

ASP.NET CORE學(xué)習(xí)教程之自定義異常處理詳解

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

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

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

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

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

1.DeveloperExceptionPageMiddleware

能給出詳細(xì)的請(qǐng)求/返回/錯(cuò)誤信息,因?yàn)榘舾行畔ⅲ詢H適合開(kāi)發(fā)環(huán)境

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

僅處理500錯(cuò)誤

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

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

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

CustomExceptionMiddleWare

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

/// <summary>
 /// 異常中間件配置對(duì)象
 /// </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關(guān)鍵字
 /// <para>僅HandleType=Both時(shí)生效</para>
 /// </summary>
 public IList<PathString> JsonHandleUrlKeys { get; set; }

 /// <summary>
 /// 錯(cuò)誤跳轉(zhuǎn)頁(yè)面
 /// </summary>
 public PathString ErrorHandingPath { get; set; }
 }

 /// <summary>
 /// 錯(cuò)誤處理方式
 /// </summary>
 public enum CustomExceptionHandleType
 {
 JsonHandle = 0, //Json形式處理
 PageHandle = 1, //跳轉(zhuǎn)網(wǎng)頁(yè)處理
 Both = 2  //根據(jù)Url關(guān)鍵字自動(dòng)處理
 }

聲明異常中間件的成員

/// <summary>
 /// 管道請(qǐng)求委托
 /// </summary>
 private RequestDelegate _next;

 /// <summary>
 /// 配置對(duì)象
 /// </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, "未授權(quán)的請(qǐng)求" },
  { 404, "找不到該頁(yè)面" },
  { 403, "訪問(wèn)被拒絕" },
  { 500, "服務(wù)器發(fā)生意外的錯(cuò)誤" }
  //其余狀態(tài)自行擴(kuò)展
  };
 }

異常中間件主要邏輯

public async Task Invoke(HttpContext context)
 {
  Exception exception = null;
  try
  {
  await _next(context); //調(diào)用管道執(zhí)行下一個(gè)中間件
  }
  catch (Exception ex)
  {
  context.Response.Clear(); 
  context.Response.StatusCode = 500; //發(fā)生未捕獲的異常,手動(dòng)設(shè)置狀態(tài)碼
  exception = ex;
  }
  finally
  {
  if (exceptionStatusCodeDic.ContainsKey(context.Response.StatusCode) && 
   !context.Items.ContainsKey("ExceptionHandled")) //預(yù)處理標(biāo)記
  {
   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關(guān)鍵字決定異常處理方式
   {
   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)一格式響應(yī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>
 /// 處理方式:跳轉(zhuǎn)網(wǎng)頁(yè)
 /// </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; //設(shè)置請(qǐng)求頁(yè)面為錯(cuò)誤跳轉(zhuǎn)頁(yè)面
  try
  {
  await _next(context); 
  }
  catch { }
  finally
  {
  context.Request.Path = originPath; //恢復(fù)原始請(qǐng)求頁(yè)面
  }
 }

使用擴(kuò)展類進(jìn)行中間件注冊(cè)

public static class CustomExceptionMiddleWareExtensions
 {

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

在Startup.cs的Configuref方法中注冊(cè)異常中間件

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

接下來(lái)我們來(lái)進(jìn)行測(cè)試,首先模擬一個(gè)將會(huì)進(jìn)行頁(yè)面跳轉(zhuǎn)的未經(jīng)捕獲的異常

訪問(wèn)/home/about的結(jié)果

訪問(wèn)/home/test的結(jié)果 (該地址不存在)

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

訪問(wèn)/api/token/gettesterror的結(jié)果

訪問(wèn)/api/token/test的結(jié)果 (該地址不存在)

訪問(wèn)/api/token/getvalue的結(jié)果 (該接口需要身份驗(yàn)證)

測(cè)試完成,頁(yè)面跳轉(zhuǎn)和統(tǒng)一格式返回都沒(méi)有問(wèn)題,自定義異常中間件已按預(yù)期工作

需要注意的是,自定義中間件會(huì)響應(yīng)每個(gè)HTTP請(qǐng)求,所以處理邏輯一定要精簡(jiǎn),防止發(fā)生不必要的性能問(wèn)題

總結(jié)

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

相關(guān)文章

最新評(píng)論