3

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?

2

2 Answers 2

4

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.

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

4 Comments

It is no solution. I have multiple properties inside SomeInput. I use only simplified code in this example.
You can do this for as many properties as you'd like. Obviously, it's tedious with a lot of fields.
Yes yo're right but I have more things in wrapping object SomeInput like validations, comments to generate documentation etc.
You can always build that object with these fields.
0

In your model you could do something like this.

using Microsoft.AspNetCore.Mvc;

public class SomeInput
{
  [FromQuery(Name = "property_name")]
  public string PropertyName {get; set;}
    
}

Then in your controller get method access it from the mapped name

[HttpGet]
public IHttpActionResult DoMethod([FromQuery]SomeInput someInput)
{
 someInput.PropertyName 
 ...
}

Comments

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.