I need to return a consistent response with a similar structure returned for all requests. In the previous .NET web api, I was able to achieve this using DelegatingHandler (MessageHandlers). The object that I want to return will be encapsulated in the Result element. So basically the json response will be in this kind of structure:
Example 1:
{
"RequestId":"some-guid-abcd-1234",
"StatusCode":200,
"Result":
{
"Id":42,
"Todo":"Do Hello World"
}
}
Example 2:
{
"RequestId":"some-guid-abcd-1235",
"StatusCode":200,
"Result":
{
[
{
"Id":42,
"Todo":"Print Hello World"
},
{
"Id":43,
"Todo":"Print Thank you"
}
]
}
}
In .NET core, it looks like I need to do this via middleware. I tried but I don't see a nicer way to extract the content like how in the previous web API when you can call HttpResponseMessage.TryGetContentValue
to get the content and wrap it in global/common response model.
How can I achieve the same in .NET core?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…