【发布时间】:2020-11-30 13:03:46
【问题描述】:
我有一个用 core 3.1 编写的 REST 服务。当出现错误时,我使用控制器中的 Problem() 方法返回一个 ProblemDetails 对象。当我通过 Swagger UI 运行它时,我得到了我所期望的。
{
"isError": true,
"responseException": {
"exceptionMessage": {
"type": "ApplicationException",
"title": "Internal Server Error",
"status": 500,
"detail": "An Error Happened. I want this message to bubble up.",
"instance": "/v1/TestAdapter/GetError",
"traceId": "|31cf435b-44b49327cac802b2."
}
}
}
当我从服务调用中得到它时,我想将它序列化回一个问题详细信息对象。但这不起作用。我一直在寻找应该序列化的对象。
我可以这样做并获取数据。
var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
但这不起作用。 (所有字段均为空。)
var typedBody = JsonConvert.DeserializeObject<ProblemDetails>(responseText, new JsonSerializerSettings());
我应该在这里做什么才能将它编组回一个 ProblemDetails 对象?
【问题讨论】:
-
ProblemDetails 类是什么样的?
-
ProblemDetails 受 Microsoft 支持。 docs.microsoft.com/en-us/dotnet/api/…您也可以继承它并添加更多特定于您的应用程序的字段。