改装 - 使用改装参数(字符串)作为控制器参数传递

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

有没有办法用Refit动态输入参数?

我的改装界面中有这段代码:

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

parm 的值为:

        "id=1234&x=567"

我的路线:

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

我得到的只是 id 参数的值为空。预期结果将是一个字符串,其值为“1234

任何帮助:D?

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

您可以使用带有所需参数的自定义 POCO class,也可以使用 Dictionary<string,string> 来传递值。该词典似乎最适合您的用例。

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.

编辑:您可以使用 HttpUtility.ParseQueryString 将您的字符串解析为 NameValueCollection,它基本上是一个字典。