c# - DelegatingHandler中如何手动返回json内容
问题描述
这是我的代表团处理程序
public class MyHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
MyCustomClass obj = GetMyCustomObject();
HttpResponseMessage response = request.CreateResponse(HttpStatusCode.OK);
response.Content = GetJsonContent(obj);
return Task.FromResult(response);
}
private HttpContent GetJsonContent(object obj)
{
throw new NotImplementedException();
}
}
我怎么能obj
作为 json 结果返回;
解决方案
你可能想试试
response.Content = new StringContent("{\"name\":\"John Doe\",\"age\":33}",
Encoding.UTF8,
"application/json");//CONTENT-TYPE header