【发布时间】:2016-10-05 06:56:22
【问题描述】:
我想知道从 WebApi 中的歧视联合的结果中返回干净的 json 的最简单方法是什么?此用例仅用于业务逻辑,不用于 http 错误等 - 那些在管道中处理并像往常一样返回给用户(404、500 等)
例如:
type ServiceResult<'a> = { Message:string; Payload:'a }
type ServiceResponse<'a> =
| Success of ServiceResult<'a>
| Fail of ServiceResult<string>
返回任一:
{
"Case": "Fail",
"Fields": [
{
"Message": "Error performing business logic, your x is not in the y.",
"Payload": "I just couldnt do it"
}
]
}
...或...
{
"Case": "Success",
"Fields": [
{
"Message": "",
"Payload": { "FirstName": "Johnny", "LastName":"Smith" }
}
]
}
我希望只返回服务结果,例如:
{
"Message": "",
"Payload": { "FirstName": "Johnny", "LastName":"Smith" }
}
...或...
{
"Message": "Error during operation due to spite.",
"Payload": "I just couldnt do it"
}
我已经尝试过 IdiomaticDuConverter: https://gist.github.com/isaacabraham/ba679f285bfd15d2f53e
但它没有工作。我见过的最接近的是 Microsoft.FSharpLu.Json,但它没有 MediaTypeFormatter 可以插入管道。
我知道我可以使用 Lu 并创建自己的 MediaTypeFormatter,但我觉得必须有一种更简单的方法(比如我缺少一些 Json.Net 选项)。
你能指出我正确的方向吗?
谢谢 :-)
【问题讨论】:
标签: f# json.net asp.net-web-api2