【问题标题】:API call with named tuple as input parameter [duplicate]以命名元组作为输入参数的 API 调用 [重复]
【发布时间】:2019-11-29 21:02:18
【问题描述】:

我的一个 API 端点需要一个包含命名元组列表的对象。输入对象的一个​​示例是:

public enum Countries { Afghanistan, Albania, Algeria, Andorra /*...*/ }

public class InputModel 
{
    public List<(Countries Country, bool Visited)> CountriesVisited { get; set; }
}

当我加载 Swagger 时,示例输入值使用命名元组,而是使用 item1item2。例如:

"countriesVisited": [
    {
        "item1": "Afghanistan",
        "item2": "false"
    }
]

当我调用端点时,似乎我必须使用item1item2 来识别已指定的值。使用命名元组(countryvisited)会导致指定的值被忽略。

是否可以更改此设置,以便当我调用端点时,我可以使用命名元组名称,而不是通用名称 item1item2

我知道我可以通过使用CountryVisited 作为属性创建一个类来解决这个问题,但是如果可能的话,我想避免为这样的事情创建一个自定义类。

【问题讨论】:

标签: c# asp.net-core namedtuple


【解决方案1】:

命名元组是一种语法糖,因此您所看到的是预期的。

更多:Using a C# 7 tuple in an ASP.NET Core Web API Controller

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 2014-07-19
    • 2016-09-21
    • 1970-01-01
    • 2020-12-29
    相关资源
    最近更新 更多