I am having an issue with binding in webapi (.net core 2.1). Usually when I use xml/json, I can add attributes to the model props (either via XmlElement or JsonProperty). I have a project however that uses application/x-www-form-urlencoded as media type and thus, the models only work if the names of the params match the property names (luckily case insensitive). Can you advise if there is a way to implement such mapping?
You can annotate your model properties with the FromForm
attribute, which has a property Name
that specifies the name to use when binding. Here's an example:
public class SomeModel
{
[FromForm(Name = "SomePropertyNameToUse")]
public string SomeProperty { get; set; }
[FromForm(Name = "SomeOtherPropertyNameToUse")]
public string SomeOtherProperty { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With