Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Api Model Binding and Polymorphic Inheritance

I am asking if anyone knows if it is possible to to pass into a Web Api a concrete class that inherits from a abstract class.

For example:

public abstract class A {     A(); }  public class B : A { }  [POST("api/Request/{a}")] public class Request(A a) { } 

At present I have looked around and most solutions seem to say that using TypeNameHandling will work.

JsonMediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter(); jsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto; 

However this is not that case. Also my model is being passed from a console app to the webapi. I have read that I may be able to deserialize the json object and after attempting this a few times I decide this was not going to work.

I have looked into creating a customer model binder however, I do not want to make my application more complex that it has to be. At present I inherit from the abstract class with 3 models but may in the future extend this. As you may note adding custom model binders may require multiple binders unless there is a way of making one binder generic for all types of the abstract class.

To expand on this in my console app I have instantiated class b as such and then passed it to the ObjectContent before posting to my webapi

item = B();  //serialize and post to web api MediaTypeFormatter formatter; JsonMediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter(); jsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto; formatter = jsonFormatter;  _content = new ObjectContent<A>(item, formatter); var response = _client.PostAsync("api/Request", _content).Result; 

when the webapi action is called the object is null

like image 419
Ian Richards Avatar asked Jun 24 '13 14:06

Ian Richards


People also ask

What is model binding in Web API?

Model Binding is the most powerful mechanism in Web API 2. It enables the response to receive data as per requester choice. i.e. it may be from the URL either form of Query String or Route data OR even from Request Body. It's just the requester has to decorate the action method with [FromUri] and [FromBody] as desired.

What is model binding in .NET core?

Model binding allows controller actions to work directly with model types (passed in as method arguments), rather than HTTP requests. Mapping between incoming request data and application models is handled by model binders.

What is FromBody in asp net core?

[FromBody] attributeThe ASP.NET Core runtime delegates the responsibility of reading the body to an input formatter. Input formatters are explained later in this article. When [FromBody] is applied to a complex type parameter, any binding source attributes applied to its properties are ignored.


1 Answers

If one really wanted to implement what is asked in the question, there is a custom way to do it.

First, create a custom json converter that is inherited from JsonConverter, in it pick a target class and deserialize an instance.

Then, in your WebApiConfig.Register you add your new converter into config.Formatters.JsonFormatter.SerializerSettings.Converters and enjoy this monstrosity in action.

Should you do it? No.

Understanding how to use such API will bring no joy to any new users, documenting this will not be easy, and most importantly - there are no benefits in implementing it this way. If input types are different, then they deserve separate API methods with different URLs. If only few properties are different - make them optional.

Why the example did not work? The TypeNameHandling is from Json.NET, Web API knows nothing about it, and type information is not part of the JSON spec, so there is no standard way to solve this particular issue.

like image 91
Dmytro Zakharov Avatar answered Sep 28 '22 22:09

Dmytro Zakharov