0

I have written a Web API to be used as a webhook with a 3rd party service so they can send me data when an event occurs in their service. They have dictated what the posted data coming in will be. So I created a class for the incoming data such that each property name matches the field names coming from the posted form urlencoded data.

What I would LIKE to do is name things in my class in a more user-friendly manner than the names they are sending in their POST request. I cannot find if there is a way, maybe using attributes, that will tell the ModelBinder which form data to put into which property. Is there a way to do this?

Incidentally, when I thought they were sending JSON as the POSTED data, I was able to do this with JSON.NET and their JsonProperty attribute. But now that it's coming as application/x-www-form-urlencoded, I don't know how to do this same thing.

Thanks for your help!

Cindy K.
  • 128
  • 2
  • 10
  • You are likely going to need to roll your own custom model binder to achieve this. This similar MVC2 question may help http://stackoverflow.com/questions/4316301/asp-net-mvc-2-bind-a-models-property-to-a-different-named-value – Mark Jones Dec 04 '12 at 14:20
  • Have you tried using the [DataContract] and [DataMember] attributes? Those work for mapping a service's property names to your property names in WCF, I would imagine they should also work for WebAPI. – danludwig Dec 04 '12 at 15:27
  • Thanks, Dan, I will give that a try. Otherwise, I will review the post Mark pointed me to for a solution. Thanks! – Cindy K. Dec 05 '12 at 17:20

1 Answers1

-2
please try something like this

[DataContract]
public class TestSerialization
{
    [DataMember(Name = "field_one")]
    public string ItemOne { get; set; }

    [DataMember(Name = "field_two")]
    public string ItemTwo { get; set; }
}
suulisin
  • 1,414
  • 1
  • 10
  • 17