0

We have the following webApi get method:

[HttpGet]
    public IEnumerable<MdmUserDto> Search([FromQuery] string[] query, [FromQuery] int limit = 30)

We are using Blazor and I need to make a call to the above. My function currently looks like:

private async Task<IEnumerable<MdmUserDto>> Search(string query)
    {
        var response = await LocalHttpClient.GetAsync(_baseAddress + "api/UserSearch/Search");
        response.EnsureSuccessStatusCode();

        using var responseContent = await response.Content.ReadAsStreamAsync();
        return await JsonSerializer.DeserializeAsync<IEnumerable<MdmUserDto>>(responseContent);
    }

But I dont know how to attach my input string to the call to meet the api's definition.

bilpor
  • 3,467
  • 6
  • 32
  • 77
  • 1
    `FromQuery` means from the query string so `?query=abc&limit=30` on the URL for example when making your call – Charleh Oct 14 '21 at 11:44
  • Yes, it does thank you. – bilpor Oct 14 '21 at 11:51
  • Great ;-) - note that there are some older solutions, afaik the answer using https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.webutilities.queryhelpers is the most recent version. – Christoph Lütjen Oct 14 '21 at 11:54

1 Answers1

1

try this url

var limit=50;
 var response = await LocalHttpClient.GetAsync(_baseAddress + 
"api/UserSearch/Search?query=" + query + "&limit=" + limit.ToStrintg());

and if your query string has an empty spaces for example or special symbols it should be encoded. YOu can do it manually or can use http utility or query string builder for example.

Serge
  • 40,935
  • 4
  • 18
  • 45