3

I have .NET Core Web API project. One of my action controller is:

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

NotifyInput.cs file is in separate project (.NET Standard):

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; }
}

And params I send to this method are:

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

I'm trying to send my data via Postman as x-www-form-urlencoded and as form-data.

But when I debug this code, I see they are all NULL.

Attribute [JsonProperty("body-plain")] didn't help me.

How can I bind these params?

6
  • How are you sending these values to the API? Commented Feb 25, 2019 at 14:14
  • Updated question Commented Feb 25, 2019 at 14:17
  • 2
    I think @DavidG meant specifically how are you sending them, like via Postman? What does the actual request body look like? Commented Feb 25, 2019 at 14:18
  • Off the bat, what you're passing for attachments won't work at all. If you're going x-www-form-urlencoded, you've got to go all the way, which means having separate params for each property of each attachment, i.e. attachments[0].Url, attachments[0].ContentType, etc. Commented Feb 25, 2019 at 14:20
  • Okay, attachments[0].url works fine, but what about body-html or attachments[0].content-type? Commented Feb 25, 2019 at 14:38

2 Answers 2

1

Try to use [ModelBinder(Name="name")] to specify a name for binding data

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; }

}
Sign up to request clarification or add additional context in comments.

1 Comment

My class NotifyInput is in separate project (.NET Standard) so I can't use Microsoft.AspNetCore.Mvc namespace here.
0

I played around with this and discovered that because you're passing a x-www-form-urlencoded to the endpoint, setting [JsonProperty("body-plain")] won't work; because you aren't sending Json.

So to pass those values you have to use the exact names on the models (case insensitive, but no hyphens). So, passing the following

bodyplain:123
bodyhtml:<p>123</p>

should work. attachments[0].content-type also has to become attachments[0].ContentType

1 Comment

Unfortunately, I can't change param names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.