1

I need to pass query parameters and path parameters to HttpClient GetAsync() method in .NET

File/{document_id}?version={version_number}

[Route("api/drs/v1/document/getdetails/{Id}")]
[HttpGet]
public async Task<HttpResponseMessage> DocumentDetails(HttpRequestMessage details)
{
    // Debugger.Launch();
    try
    {
        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Clear();

            //String path=Request.Url.GetLeftPart(UriPartial.Path);
            HttpResponseMessage response = await client.GetAsync("http://localhost:8089/api/drs/v1/document/getdetail/"]);

            if (response.IsSuccessStatusCode)
            {
                Console.Write("Success");
            }
            else
            {
                Console.Write("Failure");
            }

            return response;
        }
    }
    catch (Exception e)
    {
        throw e;
    }
}

I can't pass both the parameter in GetAsync() method

1
  • 1
    Query parameters are part of the URL, this part is called the Query string Commented Jul 29, 2019 at 6:11

1 Answer 1

1

Try to use next extension method for building your url. You can pass base url, path and dictionary of paramaters, which you need.

    public static Uri BuildUri(string baseUrl, string path, Dictionary<string, string> queryParams = null)
    {
        var uriBuilder = new UriBuilder(baseUrl) {Path = path};

        if (queryParams != null)
        {
            var query = string.Join("&", queryParams.Select(x => $"{x.Key}={x.Value}").ToArray());
            uriBuilder.Query = query;
        }

        return uriBuilder.Uri;
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.