在.Net Core 2.2中以IActionResult返回HttpResponseMessage的正确方法

时间:2019-01-10 20:32:43

标签: asp.net-core .net-core asp.net-core-2.0 asp.net-core-webapi coreclr

在.Net Core 2.2中。我正在创建一个API控制器,该API控制器根据有效负载将请求路由到另一个Http端点。

[Route("api/v1")]
public class RoutesController : Controller
{
    [HttpPost]
    [Route("routes")]
    public async Task<IActionResult> Routes([FromBody]JObject request)
    {

      var httpClient = new HttpClient();

      // here based on request httpCLient will make `POST` or `GET` or `PUT` request
      // and returns `Task<HttpResponseMessage>`. Lets assume its making `GET` 
      // call

     Task<HttpResponseMessage> response = await httpClient.GetAsync(request["resource"]);

       ??? what is the correct way to return response as `IActionResult`
    }        
}

基于SO个帖子,我可以做到

        return StatusCode((int)response.StatusCode, response);

但是,我不确定以HttpResponseMessage的身份发送ObjectResult是正确的方法。

我还想确保内容协商能够进行。

4 个答案:

答案 0 :(得分:2)

您可以创建一个自定义的IActionResult,它将包装传输逻辑。

public async Task<IActionResult> Routes([FromBody]JObject request)
{
    var httpClient = new HttpClient();

    HttpResponseMessage response = await httpClient.GetAsync("");

    // Here we ask the framework to dispose the response object a the end of the user resquest
    this.HttpContext.Response.RegisterForDispose(response);

    return new HttpResponseMessageResult(response);
}

public class HttpResponseMessageResult : IActionResult
{
    private readonly HttpResponseMessage _responseMessage;

    public HttpResponseMessageResult(HttpResponseMessage responseMessage)
    {
        _responseMessage = responseMessage; // could add throw if null
    }

    public async Task ExecuteResultAsync(ActionContext context)
    {
        context.HttpContext.Response.StatusCode = (int)_responseMessage.StatusCode;

        foreach (var header in _responseMessage.Headers)
        {
            context.HttpContext.Response.Headers.TryAdd(header.Key, new StringValues(header.Value.ToArray()));
        }

        using (var stream = await _responseMessage.Content.ReadAsStreamAsync())
        {
            await stream.CopyToAsync(context.HttpContext.Response.Body);
            await context.HttpContext.Response.Body.FlushAsync();
        }
    }
}

答案 1 :(得分:0)

public class HttpResponseMessageResult : IActionResult
{
    private readonly HttpResponseMessage _responseMessage;

    public HttpResponseMessageResult(HttpResponseMessage responseMessage)
    {
        _responseMessage = responseMessage; // could add throw if null
    }

    public async Task ExecuteResultAsync(ActionContext context)
    {
        var response = context.HttpContext.Response;


        if (_responseMessage == null)
        {
            var message = "Response message cannot be null";

            throw new InvalidOperationException(message);
        }

        using (_responseMessage)
        {
            response.StatusCode = (int)_responseMessage.StatusCode;

            var responseFeature = context.HttpContext.Features.Get<IHttpResponseFeature>();
            if (responseFeature != null)
            {
                responseFeature.ReasonPhrase = _responseMessage.ReasonPhrase;
            }

            var responseHeaders = _responseMessage.Headers;

            // Ignore the Transfer-Encoding header if it is just "chunked".
            // We let the host decide about whether the response should be chunked or not.
            if (responseHeaders.TransferEncodingChunked == true &&
                responseHeaders.TransferEncoding.Count == 1)
            {
                responseHeaders.TransferEncoding.Clear();
            }

            foreach (var header in responseHeaders)
            {
                response.Headers.Append(header.Key, header.Value.ToArray());
            }

            if (_responseMessage.Content != null)
            {
                var contentHeaders = _responseMessage.Content.Headers;

                // Copy the response content headers only after ensuring they are complete.
                // We ask for Content-Length first because HttpContent lazily computes this
                // and only afterwards writes the value into the content headers.
                var unused = contentHeaders.ContentLength;

                foreach (var header in contentHeaders)
                {
                    response.Headers.Append(header.Key, header.Value.ToArray());
                }

                await _responseMessage.Content.CopyToAsync(response.Body);
            }
        }
    }

答案 2 :(得分:-1)

ASP.NET Core具有返回对象RedirectResult,用于重定向调用方。

答案 3 :(得分:-1)

将响应简单地包装在Ok()动作返回类型中:

return Ok(response) 

因此您的代码应类似于:

[Route("api/v1")]
public class RoutesController : Controller
{
    [HttpPost]
    [Route("routes")]
    public async Task<IActionResult> Routes([FromBody]JObject request)
    {

      var httpClient = new HttpClient();
   
      Task<HttpResponseMessage> response = await httpClient.GetAsync(request["resource"]);

      return Ok(response);
    }        
}

我认为您可能需要更改

   Task<HttpResponseMessage> response = await httpClient.GetAsync(request["resource"]);

   HttpResponseMessage response = await httpClient.GetAsync(request["resource"]);

或者,如果您想返回任务(不确定为什么这样做),则可以删除 await 关键字以返回任务。

此处有更多信息:https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-3.1