Take the 2-minute tour ×

I am stuck with following problem in ASP.NET Web Api. Let say I have following code in my ApiController:

public void Post(Person person)
    // Handle the argument

What I would like to do is to accept following JSON request:

    "person": {
        "name": "John Doe",
        "age": 27

I would like to go around creating some holding object for each model just to properly bind incoming data. In previous version of MVC, it was possible to define something like Prefix to solve this.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Let me report that I have been able to solve this implementing CustomJsonMediaTypeFormatter:

public class EmberJsonMediaTypeFormatter : JsonMediaTypeFormatter
    public override System.Threading.Tasks.Task<object> ReadFromStreamAsync(
        Type type,
        System.IO.Stream readStream,
        System.Net.Http.HttpContent content,
        IFormatterLogger formatterLogger)
        return base.ReadFromStreamAsync(
            formatterLogger).ContinueWith<object>((task) =>
            var data = task.Result as JObject;
            var prefix= type.Name.ToLower();

            if (data[prefix] == null)
                return GetDefaultValueForType(type);

            var serializer = JsonSerializer.Create(SerializerSettings);

            return data[prefix].ToObject(type, serializer);

and replacing default JsonMediaTypeFormatter in GlobalConfiguration.

share|improve this answer
I've done it in a similar way once (gist.github.com/MilkyWayJoe/4422478) but resumed to use the WebAPIAdapter & Serializer. They need some work, but make it a lot easier as I don't have to change backend API in any way. –  MilkyWayJoe Apr 22 '13 at 14:18
Yes, it depends which side you want (or even can) bend. –  Myslik Apr 22 '13 at 15:59
add comment

Your Answer


