小编Jan*_*ane的帖子

如何通过 Refit 和其他来源在 Web API 应用程序中将字典作为查询参数传递?

我在将 Dictionary 作为父对象的一部分作为查询参数传递时遇到了一些问题。

因此,我有一个获取 FormulationParameters 变量的方法。

public async ValueTask<IActionResult> Get([FromQuery] FormulationParameters formulationParameters)
{
     return Ok(await _service.Get(formulationParameters));
}
Run Code Online (Sandbox Code Playgroud)

FormulationParameters 类如下所示:

public class FormulationParameters
{
   //other simple properties, e.g. string, int
        
   public Dictionary<string, string> Substitute { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

当我使用 Postman 传递这些参数时,我像这样填写了“查询参数”表单,一切正常:

  • 我将一个键作为Substitute[FirstValue]传递,其值作为10

Substitute字典中,我得到一条记录,其中 Key 等于“FirstValue”,其 Value 为10

但是当我尝试通过 Refit 为我的 API 编写一些测试时,我发现这并没有像我预期的那样工作。

所以,我在Refit接口中有一个方法:

[Get("/api/path")]
Task<HttpResponseMessage> GetFormulations([Query] FormulationParameters parameters]
Run Code Online (Sandbox Code Playgroud)

我在代码中填写了 FormulationParameters 的所有属性。当我调试我的测试时,我看到在 API 方法(上面定义的)中,formulationParameters变量的所有属性都被填充,除了这个 Substitute 字典。

我在网上寻找这个问题并找到了一些解决方案。我尝试了所有这些,但对我不起作用。

首先,我尝试对此属性使用[JsonDataExtension]属性。正如我所读到的,这意味着如果您将此属性设置为某些属性,则来自 …

c# dictionary refit

6
推荐指数
0
解决办法
959
查看次数

标签 统计

c# ×1

dictionary ×1

refit ×1