c# - retrofit - 使用 retrofit 参数(字符串)作为 Controller 参数传递

标签 c# asp.net-core refit

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

我的 Refit 界面中有这段代码:

      [Get("/click?{parm}")]
      Task<ApiResponse<TAny>> SaveClick(string 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 类,也可以使用 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,它基本上是一个字典。

关于c# - retrofit - 使用 retrofit 参数(字符串)作为 Controller 参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70725642/

相关文章:

c# - WebBrowser 控件和 Wininet API

c# - 是否可以只在 ASP.Net 中处理 404 错误?

c# - 将表单数据发布到 MVC Core API

internet-explorer - 缓存控制 : no-store header being overwritten by . 网络核心 CSRF token 生成器

c# - retrofit 和授权 header

c# - 从 .NET 程序中获取 swf 文件的尺寸

c# - C# 中的所有引用类型都是类类型吗?

使用 .NET Core 的 Azure Apps EasyAuth 声明

asp.net-core - 将范围服务注入(inject) DelegatingHandler 抛出 InvalidOperationException