gpt4 book ai didi

c# - 绑定(bind)属性 FromForm .NET Core Web API

转载 作者:行者123 更新时间:2023-11-30 18:12:52 26 4
gpt4 key购买 nike

我有 .NET Core Web API 项目。我的 Action Controller 之一是:

[HttpPost]
public async Task<IActionResult> Notify([FromForm] NotifyInput input)
{ ... }

NotifyInput.cs 文件位于单独的项目中(.NET 标准):

public string BodyPlain { get; set; }

public string BodyHtml { get; set; }

public List<Attachment> Attachments { get; set; }

public class Attachment
{
public int Size { get; set; }

public string Url { get; set; }

public string Name { get; set; }

public string ContentType { get; set; }
}

我发送给这个方法的参数是:

body-plain: 123
body-html: <p>123</p>
attachments: [{"url": "http://example.com", "content-type": "image/jpeg", "name": "pexels-photo.jpg", "size": 62169}]

我正在尝试通过 Postman 以 x-www-form-urlencodedform-data 的形式发送我的数据。

但是当我调试这段代码时,我发现它们都是NULL

属性 [JsonProperty("body-plain")] 没有帮助我。

如何绑定(bind)这些参数?

最佳答案

尝试使用[ModelBinder(Name="name")]为绑定(bind)数据指定名称

public class NotifyInput
{
[ModelBinder(Name = "body-plain")]
public string BodyPlain { get; set; }
[ModelBinder(Name = "body-html")]
public string BodyHtml { get; set; }

public List<Attachment> Attachments { get; set; }
}
public class Attachment
{
public int Size { get; set; }

public string Url { get; set; }

public string Name { get; set; }

[ModelBinder(Name = "content-type")]
public string ContentType { get; set; }

}

关于c# - 绑定(bind)属性 FromForm .NET Core Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54868081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com