【问题标题】:API with an parameter named with keyword word in c#在 C# 中带有以关键字 word 命名的参数的 API
【发布时间】:2022-01-22 13:29:52
【问题描述】:

从外部公司调用我们的 API 之一使用参数名称“ref”。他们要求我们创建接受此参数的 web api。我们正在用 C# Web Api 编写,“ref”是一个关键字,无法做到这一点。有什么解决办法吗?

https://xxxxxxxxx/xxx/xxx/xxxxx/?ref=1234

【问题讨论】:

    标签: c# .net web-services webapi


    【解决方案1】:

    您可以在字段前使用@ 符号接受ref 作为参数:

    public JsonResult MyMethod(string @ref)
    

    您可以阅读更多here

    【讨论】:

    • 已经投票了...可以在几分钟后标记为答案...将def do..
    【解决方案2】:

    你也可以使用FromUriAttributeName属性:

    public JsonResult MyMethod([FromUri(Name = "ref")] string reference)
    

    【讨论】:

    • 谢谢你..又学到了一个新东西...
    猜你喜欢
    • 1970-01-01
    • 2021-08-24
    • 2019-02-12
    • 2013-12-10
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    相关资源
    最近更新 更多