25

In .Net Core 2.2. I am creating a API Controller that routes the request to another Http endpoint based on payload.

  [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`*/
      }        
  }

based on SO post i can do this

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

However i am not sure sending HttpResponseMessage as ObjectResult is correct way.

I also want to make sure content negotiation will work.

Ramon Dias
  • 372
  • 1
  • 4
  • 14
LP13
  • 20,711
  • 38
  • 136
  • 286
  • You can't return an HttpResponseMessage object like in previous web api framework. Instead, you can create a custom IActionResult (like HttpResponseMessageResult) that will copy statuscode, headers and body to the httpContext.Response in the ActionResult's ExecuteResultAsync method – Kalten Jan 10 '19 at 22:07
  • any example? The backend http api may return json result or stream. so the `HttpResponseMessage` will have that information as `HttpContent` – LP13 Jan 10 '19 at 22:15
  • Were you able to figure this out? – rboy Mar 23 '21 at 17:48
  • ye I posted my answer below https://stackoverflow.com/a/54187518/3862378 – LP13 Mar 23 '21 at 19:44

4 Answers4

26
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);
            }
        }
    }
LP13
  • 20,711
  • 38
  • 136
  • 286
  • 4
    Not sure why you got a -1, this actually worked, with the exception of changing `Headers.Append` to `Headers.TryAdd` in my .net core 2.2 project – pvenky May 30 '19 at 20:20
  • just wanted to add a comment that this worked in .Net core 3.1, not like the accepted answer. – Eric Tijerina Jun 03 '20 at 23:48
  • It works with core 3.1! And if you add `using Microsoft.AspNetCore.Http;` it will resolve the `Headers.Append` extension method. – Isolin Jun 13 '20 at 23:53
  • Is there a better way? Not to use that "unused" variable? – Thor88 Aug 07 '20 at 03:11
  • @Thor88 use discard identifier to get rid of unused variable warnings i.e. `var _ = contentHeaders.ContentLength;` – zafar Feb 01 '21 at 16:15
24

You can create a custom IActionResult that will wrap transfere logic.

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();
        }
    }
}
Kalten
  • 3,398
  • 18
  • 28
  • i was looking at https://github.com/aspnet/Mvc/blob/release/2.2/src/Microsoft.AspNetCore.Mvc.WebApiCompatShim/ResponseMessageResult.cs and https://github.com/aspnet/Mvc/blob/release/2.2/src/Microsoft.AspNetCore.Mvc.WebApiCompatShim/ApiController.cs at line no 523. Not sure thats going to work. Its basically returning `HttpReponseMessage` as `Response` property of ObjectResult. I will try your method as well – LP13 Jan 10 '19 at 22:39
  • In webapishim they use custom formater (https://github.com/aspnet/Mvc/blob/release/2.2/src/Microsoft.AspNetCore.Mvc.WebApiCompatShim/Formatters/HttpResponseMessageOutputFormatter.cs) that you probably need to register somewhere. This is a more complete implementation than mine. – Kalten Jan 10 '19 at 22:45
  • Yep but as it's open sourced, you can get inspired by the part you need without every other compatibility layers. – Kalten Jan 10 '19 at 23:21
  • 1
    i used your approach of creating IActionResult, however i used the code from https://github.com/aspnet/Mvc/blob/release/2.2/src/Microsoft.AspNetCore.Mvc.WebApiCompatShim/Formatters/HttpResponseMessageOutputFormatter.cs inside `ExecuteResultAsync`.. see my post below – LP13 Jan 14 '19 at 18:55
0

ASP.NET Core has the return object RedirectResult to redirect the caller.

Richard Fuller
  • 424
  • 3
  • 12
  • First, the link you provided implements `IHttpActionResult` not `IActionResult`. I know .net core has same objects. However redirect is used to redirect user to a particular url. My question is related to API not a Web Site. You dont redirect from API – LP13 Jan 14 '19 at 19:52
  • I incorrectly remembered IHttpActionResult implementing IActionResult. In that case, [RedirectResult](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.redirectresult?view=aspnetcore-2.2) is what you want. – Richard Fuller Jan 14 '19 at 20:07
  • How do you redirect a POST request? Or manage authenticated request without sending credentials to the user? – Kalten Jan 14 '19 at 20:51
  • Does RedirectResult not support POST requests? And I'm not sure what you mean in the second question. – Richard Fuller Jan 14 '19 at 22:41
  • Sometime the resource you want to forward need credential that you must not send to the request's user. In this case you can't send a redirect response but act as proxy server. Other use case, the target resource is in a network not reachable by the user. – Kalten Jan 14 '19 at 23:47
-1

Simply wrap the response in Ok() Action return type:

return Ok(response) 

so your code would look something like:

[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);
    }        
}

And I think you might need to change

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

to

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

Alternatively, if you want to return the Task (not sure why you would), you could remove the await keyword to return the Task.

More info here: https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-3.1