Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Web API model binding - different parameters names

Is it possible to map parameters from request to properties with different names? I need it because I'd like to use words splitted by underscore as url parameters but in C# code I'd like to use normal convention. Example :

?property_name=1 to property PropertyName

In the request I use [FromUri] parameter like

public IHttpActionResult DoMethod([FromUri(Name = "")] SomeInput input)

Initially I thought that model binding is performed by Json serializer but probably it isn't. I tried DataMember attribute as well but these approaches do not work.

public class SomeInput
{
    [JsonProperty("property_name")]
    [DataMember(Name = "property_name")]
    public int PropertyName { get; set; }
}

I read about the custom binders but I hope some more simple way must exist. Any idea how to do this correctly and simple in ASP.NET Web API 2 with using Owin and Katana?

like image 267
Jozef Cechovsky Avatar asked Jul 20 '26 13:07

Jozef Cechovsky


1 Answers

You can do a remapping for an individual parameter using the Name property on [FromUri]:

public IHttpActionResult DoMethod([FromUri(Name = "property_name")] int propertyName)

To remap inside a custom object you will need to create a model binder.

like image 191
Dan Harms Avatar answered Jul 23 '26 01:07

Dan Harms



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!