Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handing Get/{id} vs Get/id with flurl

I'm trying to get Flurl to work and I'm stuck on how gets work when passing in an ID.

[HttpGet("Get/{id}")]
public IActionResult Get(int id)
{
    // Some something and return
}

The above expects

Get/1

So in Flurl:

var result = await _baseUrl
    .AppendPathSegment("/Get")
    .SetQueryParam("id", id)
    .GetJsonAsync();

This produces this:

/Get?id=8

...which then fails with a 404.

How can I get Flurl to set a query param that is /id or get my get to accept both Get/id and Get?id=

I can do the following but it doesn't seem very elegant

var result = await _baseUrl
    .AppendPathSegment(string.Format("/Get/{0}", id))
    .GetJsonAsync();
like image 447
Michael Esteves Avatar asked Sep 03 '25 02:09

Michael Esteves


1 Answers

SetQueryParam will add the value as a query string but you need the value to be part of the path. Instead consider using the AppendPathSegments method, for example:

var result = _baseUrl
    .AppendPathSegments("get", id)
    .GetJsonAsync();
like image 151
DavidG Avatar answered Sep 04 '25 16:09

DavidG