You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

REFIT构造查询时如何忽略类中的指定公共属性

解决方案

完全可以不拆分模型使用同一个TestClass实现需求,有两种成熟的实现方案:

方案1:使用Refit原生[QueryIgnore]特性(推荐,适配Refit v6.0及以上版本)

该特性为Refit专属特性,仅作用于类作为查询参数的序列化拼接阶段,不会影响接口响应的JSON反序列化逻辑,只需给不需要拼接的属性加标记即可:

public class TestClass {
    [AliasAs("myprop1")]
    public string Prop1 {get; set;}
    [AliasAs("myprop2")]
    public string Prop2 {get; set;}
    // 标记后该属性不会被拼接到查询参数中
    [QueryIgnore]
    public string Prop3 {get; set;}
    [QueryIgnore]
    public string Prop4 {get; set;}
}

修改后原有接口定义、调用逻辑、反序列化逻辑都不需要调整,符合你的需求。

方案2:低版本Refit兼容方案

如果你使用的Refit版本低于v6.0没有内置[QueryIgnore]特性,可通过匿名类传参的方式实现,不需要修改原有TestClass定义:

  1. 调整接口的Check方法参数类型为object,添加[Query]特性标记:
public interface IMyClient {
    [Get("/login")]
    public Task<ApiResponse<TestClass>> Login(string username, string password);

    [Get("/verify")]
    public Task<ApiResponse<TestClass>> Check([Query] object queryParams);
}
  1. 调用时仅传入需要的属性即可:
var loginResp = client.Login("user", "test");
var verifyResp = client.Check(new { 
    myprop1 = loginResp.Content.Prop1, 
    myprop2 = loginResp.Content.Prop2 
});

内容的提问来源于stack exchange,提问作者Marco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 18:48:03