Как вручную вернуть json контент в DelegatingHandler - PullRequest
0 голосов
/ 04 марта 2020

Вот мой обработчик делегирования

    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 результат;

1 Ответ

0 голосов
/ 04 марта 2020

Вы можете попробовать

response.Content = new StringContent("{\"name\":\"John Doe\",\"age\":33}",
                                    Encoding.UTF8, 
                                    "application/json");//CONTENT-TYPE header
...