Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model properties custom names with url-formencoded

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?

like image 756
Daniel Stoyanoff Avatar asked Dec 18 '22 19:12

Daniel Stoyanoff


1 Answers

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; }
}
like image 185
Kirk Larkin Avatar answered Jan 13 '23 03:01

Kirk Larkin