Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using System.Threading.Tasks;
namespace GatewayAPI
{
public class HttpResponseMessageResult : IActionResult
{
private readonly HttpResponseMessage responseMessage;
public HttpResponseMessageResult(HttpResponseMessage responseMessage)
{
this.responseMessage = responseMessage ?? throw new ArgumentNullException(nameof(responseMessage));
}
public async Task ExecuteResultAsync(ActionContext context)
{
HttpContext httpContext = context.HttpContext;
HttpResponse response = httpContext.Response;
response.ContentType = "application/json; charset=utf-8";
// Copy the status code
response.StatusCode = (int)responseMessage.StatusCode;
// Copy the cookies
if (responseMessage.Headers.TryGetValues("Set-Cookie", out var cookieValues))
{
foreach (string cookie in cookieValues)
response.Headers.Append("Set-Cookie", cookie);
}
// Copy the response body directly to the response stream
using (Stream responseStream = await responseMessage.Content.ReadAsStreamAsync())
{
await responseStream.CopyToAsync(response.Body);
await response.Body.FlushAsync();
}
}
}
}