Home > Mobile >  Refit - Use refit parameters (string) to pass as controller parameters
Refit - Use refit parameters (string) to pass as controller parameters

Time:01-17

There is a way to use Refit to input parameters in a dynamic way?

I have this code in my Refit`s Interface:

      [Get("/click?{parm}")]
      Task<ApiResponse<TAny>> SaveClick(string parm);

the value of parm is:

        "id=1234&x=567"

my route:

        [HttpGet]
        [Route("click")]
        public  void test ([FromQuery] string id)
        {
            Ok("Ok");
        }

All Im getting is that the value from id parameter is null. The expected result would be a string with value "1234"

Any help :D?

CodePudding user response:

https://github.com/reactiveui/refit#dynamic-querystring-parameters

You can either use a custom POCO class with the parameters you need, or you can use a Dictionary<string,string> to pass the values. The dictionary seems to best fit your usecase.

public class MyQueryParams
{
    [AliasAs("order")]
    public string SortOrder { get; set; }

    public int Limit { get; set; }

    public KindOptions Kind { get; set; }
}

public enum KindOptions
{
    Foo,

    [EnumMember(Value = "bar")]
    Bar
}


[Get("/group/{id}/users")]
Task<List<User>> GroupList([AliasAs("id")] int groupId, MyQueryParams params);

[Get("/group/{id}/users")]
Task<List<User>> GroupListWithAttribute([AliasAs("id")] int groupId, [Query(".","search")] MyQueryParams params);


params.SortOrder = "desc";
params.Limit = 10;
params.Kind = KindOptions.Bar;

GroupList(4, params)
>>> "/group/4/users?order=desc&Limit=10&Kind=bar"

GroupListWithAttribute(4, params)
>>> "/group/4/users?search.order=desc&search.Limit=10&search.Kind=bar"

A similar behavior exists if using a Dictionary, but without the advantages of the AliasAs attributes and of course no intellisense and/or type safety.

edit: You can probably use HttpUtility.ParseQueryString to parse your string into a NameValueCollection, which is basically a Dictionary.

  •  Tags:  
  • Related