我通过HttpResponseException从WebApi返回自定义异常。例如:
//Base class for all custom app exceptions
public class MyAppException : Exception {}
//Custom exceptions
public class MyCustomException1 : MyAppException { //custom properties}
public class MyCustomException2 : MyAppException { //other custom properties}
客户端是HttpClient,返回的消息如下所示:
{"Message":"An error has occurred.","ExceptionMessage":"Exception of type 'MyApp.Exceptions.MyCustomException1 ' was thrown.","ExceptionType":"MyApp.Exceptions.MyCustomException1 ","StackTrace":null}
这里有两个问题:
有没有办法将邮件反序列化为正确的异常?
答案 0 :(得分:0)
使用Custom model binder
分析ExceptionType
属性以创建适当异常的实例。
请查看此工作Github sample
<强> EDITED 强>