6

Suppose I have a model:

public class Menu
{
    public string Name { get; set; }
    public IMenuCommand Next { get; set; }
}

IMenuCommand could have different implementations, like:

public class NextStepCommand : IMenuCommand
{
    public int Step { get; set; }
}

public class VoiceCommand : IMenuCommand
{
    public string Message { get; set; }
}

And I want to POST menus with different commands to the ASP.NET Web API service. How can I do that?

The request below will create an object with specified Name, but Next command will be null:

POST http://localhost/api/menus: {"name":"bob","next":{"step":1}}
Returns 201: {"Name":"bob","Next":null}

Default Web API binders can't map my request params to the needed C# type - of course it's a tricky part. Can I use some "known-type" attribute for interface-based properties or is there any other approach to handle this case, probably a custom model binder?

1 Answer 1

5

I think what you're looking for is Json.NET's support for type name handling. It allows you to specify the type to deserialize into by adding the "$type" json tag. You can try this code out to see how it works:

Console.WriteLine(JsonConvert.DeserializeObject<Menu>(
    @"{
         ""name"":""bob"",
         ""next"":
         {
           ""$type"" : ""ConsoleApplication.NextStepCommand,ConsoleApplication"",
           ""step"" : 1
         }
    }",
    new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto }).Next);

You'll have to replace the namespace and assembly name with your own, but you should see the NextStepCommand being correctly deserialized.

In WebAPI, you'll need to tweak your request to add the "$type" type information, and you'll need to enable TypeNameHandling like this:

config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
Sign up to request clarification or add additional context in comments.

5 Comments

Worked for me when I set TypeNameHandling.Auto for Web API JSON serializer and sent a request with needed "$type" of a command. But can I turn on TypeNameHandling feature only for deserialization? Because now I have too much garbage in JSON output in GET services.
Found that it's possible to use [JsonProperty(TypeNameHandling = TypeNameHandling.All)] for specific model properties (from Json.NET blog). Checked with Web API - it works. Much better now.
@Youssef Moussaoui - Any ideas on this question: stackoverflow.com/questions/20252027/… ... Not having any luck with anything!
@youssef - "you'll need to tweak your request to add the "$type" type information" --> please explain more how to implement this in WEB API ?
I added TypeNameHandling.Auto to my WebApiConfig for my Asp.Net project, but I don't see it working. However, it tested out with JsonConverter with this setting, it does work. What did I miss?

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.