0

I have a controller action inside which I want to take the request 'as is', overwrite just its base URL to a different one (while keeping the rest of its properties such as method, query string, form, cookies, form data etc.), fire it against the overwritten location, get the result back and return it from my controller action.

I want to avail of the asp.net model binding (as I don't know how exactly the parameters are going to be passed in), I don't want to deal with HttpClient, it's also essential to do everything from inside the controller action. Is there a way to accomplish this with asp.net core (.net core 5 I'm using).

JsCoder
  • 117
  • 1
  • 9
  • Please add your code from the controller. and client code – tire0011 Dec 27 '21 at 17:03
  • I have a working solution using HttpClient and JsonConvert.Deserilize(), with a lot of plumbing code. I hoped asp.net core provides with a more elegant solution for this problem. No client code as such just want to get it working in postman. – JsCoder Dec 27 '21 at 17:06
  • https://stackoverflow.com/a/41320302/11808788 does this help you? – Charles Dec 27 '21 at 18:53

1 Answers1

1
public async Task<IActionResult> Redirect(string property)
{
  var httpContextRequest = HttpContext.Request;
  var uri = new UriBuilder(httpContextRequest.Scheme, "AnotherHost", httpContextRequest.Host.Port.Value,
    httpContextRequest.Path).Uri;
  var httpClient = new HttpClient();
  var result = await httpClient.GetAsync(uri);
  return new JsonResult(result);
}
tire0011
  • 1,048
  • 1
  • 11
  • 22
  • it can be get put post or any other valid method. those switches while doable don't look elegant. Also requests can have cookies, form data, headers - all these needs to be taken into consideration when going HttpClient route. – JsCoder Dec 27 '21 at 17:49