0

I'm trying to create a rudimentary API gateway, that forwards requests to api services. In this example, there are 4 query string params, that would need to be included in the pass. Is there any way to avoid rebuilding the query string, and simply rerouting the request to the service?

public class WipController : Controller
{
    public readonly string BaseServiceUrl = "http://localhost:48535";
    [AllowAnonymous]
    [HttpGet("api/wip/timekeeper/{timekeeper}/clients")]
    public async Task<JsonResult> Get(string timekeeper, string endDate, string sortBy, string sortDirection)
    {
        using (var client = new HttpClient())
        {
            var url = new Uri(string.Format("{0}/api/wip/timekeeper/" + timekeeper + "/clients", BaseServiceUrl));

            var response = await client.GetAsync(url);
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception("There was an error retrieving the set of clients for the timekeeper.");
            }

            var result = await response.Content.ReadAsStringAsync();

            return Json(new { result });
        }
    }
}
user1093111
  • 1,091
  • 5
  • 21
  • 47
  • How about `var url = Request.Url.AbsoluteUri;` – Hackerman Dec 27 '17 at 17:56
  • @Hackerman Thanks. That's what i needed. However, I forgot to specify I was using Core, so I just had to use `Request.Path` and `Request.QueryString` – user1093111 Dec 27 '17 at 18:36
  • Take a look at the AbsoluteUri answer https://stackoverflow.com/questions/38437005/how-to-get-current-url-in-view-in-asp-net-core-1-0 – Hackerman Dec 27 '17 at 21:39

0 Answers0