2

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?

Jozef Cechovsky
  • 2,949
  • 2
  • 27
  • 43
  • Did you make some changes in your web api configuration? like: formatters, converters etc – MaKCbIMKo May 02 '16 at 15:38
  • Does this answer your question? [Changing the parameter name Web Api model binding](https://stackoverflow.com/q/26600275/5815327) This answer specifically worked for me: https://stackoverflow.com/a/29090053/5815327 – Deantwo Aug 28 '20 at 07:00

1 Answers1

3

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.

Dan Harms
  • 4,501
  • 2
  • 17
  • 28