日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何友好的处理 WebApi 中抛出的错误

發布時間:2023/12/4 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何友好的处理 WebApi 中抛出的错误 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

微軟的 ASP.NET Web API 是一個輕量級的web框架,可用來構建基于 http 無狀態的rest服務,異常是一種運行時錯誤,異常處理是一種處理運行時錯誤的技術,每一個開發者都應該知道如何處理 Web API 中的異常,并且在 Action 中使用合適的 錯誤碼 和 錯誤信息 進行包裝。

WebAPI 中的 HttpResponseException

你可以在 Action 中使用 HttpResponseException 來包裝指定的 HttpCode 和 HttpMessage,如下例子所示:

public?Employee?GetEmployee(int?id) {Employee?emp?=?employeeRepository.Get(id);if?(emp?==?null){var?response?=?new?HttpResponseMessage(HttpStatusCode.NotFound){Content?=?new?StringContent("Employee?doesn't?exist",?System.Text.Encoding.UTF8,?"text/plain"),StatusCode?=?HttpStatusCode.NotFound}throw?new?HttpResponseException(response);}return?emp; }

如果你的 Action 返回的是 IHttpActionResult,那么可將 GetEmployee() 方法修改如下:

public?IHttpActionResult?GetEmployee(int?id) {Employee?emp?=?employeeRepository.Get(id);if?(emp?==?null){var?response?=?new?HttpResponseMessage(HttpStatusCode.NotFound){Content?=?new?StringContent("Employee?doesn't?exist",?System.Text.Encoding.UTF8,?"text/plain"),StatusCode?=?HttpStatusCode.NotFound}throw?new?HttpResponseException(response);}return?Ok(emp); }

從上面的代碼可以看出,錯誤碼 和 錯誤消息 都賦給了 Response 對象,然后包裝到了 HttpResponseException 進行返回。

WebAPI 中使用 HttpError

除了直接實例化 HttpResponseMessage 類,還可以使用 Request.CreateErrorResponse() 快捷的創建 HttpResponseMessage 類,如下代碼所示:

public?IActionResult?GetEmployee(int?id) {Employee?emp?=?employeeRepository.Get(id);if?(emp?==?null){string?message?=?"Employee?doesn't?exist";throw?new?HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound,?message));}return?Ok(emp); }

WebAPI 中使用 異常過濾器

異常過濾器是一種可以在 WebAPI 中捕獲那些未得到處理的異常的過濾器,要想創建異常過濾器,你需要實現 IExceptionFilter 接口,不過這種方式比較麻煩,更快捷的方法是直接繼承 ExceptionFilterAttribute 并重寫里面的 OnException() 方法即可,這是因為 ExceptionFilterAttribute 類本身就實現了 IExceptionFilter 接口,如下代碼所示:

[AttributeUsage(AttributeTargets.Class?|?AttributeTargets.Method,?Inherited?=?true,?AllowMultiple?=?true)]public?abstract?class?ExceptionFilterAttribute?:?FilterAttribute,?IExceptionFilter,?IFilter{protected?ExceptionFilterAttribute();public?virtual?void?OnException(HttpActionExecutedContext?actionExecutedContext);public?virtual?Task?OnExceptionAsync(HttpActionExecutedContext?actionExecutedContext,?CancellationToken?cancellationToken);}

下面的代碼片段展示了如何通過重寫 ExceptionFilterAttribute.OnException() 方法來創建一個自定義異常過濾器,請注意下面的代碼是如何捕獲在 Action 中拋出的異常,并將捕獲到的異常轉換為 HttpStatusResponse 實體,然后塞入合適的 httpcode 和 httpmessage,如下代碼所示:

public?class?CustomExceptionFilter?:?ExceptionFilterAttribute{public?override?void?OnException(HttpActionExecutedContext?actionExecutedContext){HttpStatusCode?status?=?HttpStatusCode.InternalServerError;String?message?=?String.Empty;var?exceptionType?=?actionExecutedContext.Exception.GetType();if?(exceptionType?==?typeof(UnauthorizedAccessException)){message?=?"Access?to?the?Web?API?is?not?authorized.";status?=?HttpStatusCode.Unauthorized;}else?if?(exceptionType?==?typeof(DivideByZeroException)){message?=?"Internal?Server?Error.";status?=?HttpStatusCode.InternalServerError;}else{message?=?"Not?found.";status?=?HttpStatusCode.NotFound;}actionExecutedContext.Response?=?new?HttpResponseMessage(){Content?=?new?StringContent(message,?System.Text.Encoding.UTF8,?"text/plain"),StatusCode?=?status};base.OnException(actionExecutedContext);}}

接下來將自定義的異常過濾器添加到 HttpConfiguration 全局集合中,如下代碼所示:

public?static?void?Register(HttpConfiguration?config){config.MapHttpAttributeRoutes();config.Routes.MapHttpRoute(name:?"DefaultApi",routeTemplate:?"api/{controller}/{id}",defaults:?new?{?id?=?RouteParameter.Optional?});config.Formatters.Remove(config.Formatters.XmlFormatter);config.Filters.Add(new?CustomExceptionFilter());}

除了將自定義異常設置到全局上,你還可以縮小粒度到 Controller 或者 Action 級別上,下面的代碼分別展示了如何將其控制在 Action 和 Controller 上。

[DatabaseExceptionFilter] public?class?EmployeesController?:?ApiController {//Some?code }[CustomExceptionFilter]public?IEnumerable<string>?Get(){throw?new?DivideByZeroException();?}

ASP.NET Web API 提供了強大的 HttpResponseException 來包裝異常信息,默認情況下,當 WebAPI 中拋出異常,系統默認使用 Http StateCode = 500 作為回應,也即:Internal Server Error. ,場景就來了,如果你會用 HttpResponseException 的話,就可以改變這種系統默認行為,自定義錯誤碼和錯誤信息讓結果更加清晰語義化。

譯文鏈接:https://www.infoworld.com/article/2994111/how-to-handle-errors-in-aspnet-web-api.html

總結

以上是生活随笔為你收集整理的如何友好的处理 WebApi 中抛出的错误的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。