Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9004997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:01:56+00:00 2026-06-16T01:01:56+00:00

Suppose I have a model: public class Menu { public string Name { get;

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:01:58+00:00Added an answer on June 16, 2026 at 1:01 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have ViewModel like public class AnotherViewModel { public string Name { get;
Suppose, I have models: public class Person { public sting Name {get;set;} public List<Book>
Suppose you have a viewModel: public class CreatePersonViewModel { [Required] public bool HasDeliveryAddress {get;set;}
Suppose I have this model: public class ViewModel { [Required] public string UserInput {
Suppose I have this View Model: public class CreateGameViewModel { [Required] public string SideA
suppose I have a domain classes: public class Country { string name; IList<Region> regions;
Suppose I have a simple model, such as Record: @Model public class Record {
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a
Suppose I have my models set up already. class books(models.Model): title = models.CharField... ISBN
Suppose I have following class @Entity public class Customer extends Model { @Id public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.