【问题标题】:Model properties custom names with url-formencoded带有 url-formencoded 的模型属性自定义名称
【发布时间】:2018-12-26 18:34:19
【问题描述】:

我在 webapi (.net core 2.1) 中遇到了绑定问题。 通常当我使用 xml/json 时,我可以将属性添加到模型道具(通过 XmlElement 或 JsonProperty)。 但是,我有一个项目使用 application/x-www-form-urlencoded 作为媒体类型,因此,模型仅在参数名称与属性名称匹配时才有效(幸运的是不区分大小写)。您能否建议是否有实现此类映射的方法?

【问题讨论】:

    标签: c# asp.net-web-api serialization asp.net-core asp.net-core-mvc


    【解决方案1】:

    您可以使用FromForm 属性注释您的模型属性,该属性有一个属性Name,指定绑定时使用的名称。这是一个例子:

    public class SomeModel
    {
        [FromForm(Name = "SomePropertyNameToUse")]
        public string SomeProperty { get; set; }
    
        [FromForm(Name = "SomeOtherPropertyNameToUse")]
        public string SomeOtherProperty { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-07
      • 1970-01-01
      • 2016-07-26
      • 2020-04-14
      • 2020-01-24
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      相关资源
      最近更新 更多