WebApi 異常處理解決方案

2022-02-16 15:27:13 字數 2027 閱讀 3991

1.繼承exceptionfilterattribute類,重寫onexception方法

public class webapiexceptionfilterattribute : exceptionfilterattribute

;string errmsg = jsonconvert.serializeobject(err);

//系統異常碼

//系統異常碼

3.例子:

控制器:

}結果:

提公升:為了滿足每個webapi專案都應用自定義異常,把異常類封裝到程式集中使用

1.結構:

問題:yesway.webapi異常丟擲類庫新增引用需與webapi專案用的引用需相同,

之前的這些我都是在引用管理器,程式集中乙個個找的,新增的,出現進不了onexception方法的問題,用nuget新增的就好了,可能是版本不一樣吧

具體有這些東東:(他們之間有依賴,不需要乙個個加,加乙個可能會出來好幾個...)

webapi需要引用的包:

microsoft.aspnet.webapi

microsoft.aspnet.webapi.client

microsoft.aspnet.webapi.client.zh-hans

microsoft.aspnet.webapi.core

microsoft.aspnet.webapi.core.zh-hans

microsoft.aspnet.webapi.webhost

microsoft.aspnet.webapi.webhost.zh-hans

microsoft.codedom.providers.dotnetcompilerplat

microsoft.net.compilers

newtonsoft.json

2.webapi專案引用程式集

3.可以在控制器,控制器方法加特性使用

global中加這句,所有的控制器就都加上了

WebApi 異常處理解決方案

public class webapiexceptionfilterattribute exceptionfilterattribute else if actionexecutedcontext.exception is timeoutexception 這裡可以根據專案需要返回到客戶端特定的狀態...

WebApi異常處理解決方案

一 使用異常篩選器捕獲所有異常 public class webapiexceptionfilterattribute exceptionfilterattribute else if actionexecutedcontext.exception is timeoutexception 這裡可以根...

Springboot異常錯誤處理解決方案詳解

1.在有模板引擎的情況下 springboot會預設找 templates error 錯誤狀態碼.html,所以我們要定製化錯誤頁面就可以到templates error下建立乙個 對應錯誤狀態碼.html html檔案,當發生此狀態碼的錯誤springboot就會來到對應的頁面。同時如果我們想讓...