Deserialising Json to derived types in Asp.Net Web API

Posted by IoChaos on Stack Overflow See other posts from Stack Overflow or by IoChaos
Published on 2012-09-28T11:01:11Z Indexed on 2014/06/03 9:25 UTC
Read the original article Hit count: 202

Filed under:
|
|

I'm calling a method of my WebAPI sending a json that I would like to match (or bind) with a model.

In the controller I have a method like:

public Result Post([ModelBinder(typeof(CustomModelBinder))]MyClass model);

'MyClass', wich is given as a parameter is an abstract class. I would like that at, depending of the type of json passed, the correct inherited class is instantiated.

To achieve it, I'm trying to implement a custom binder. The problem is that (I don't know if it's very basic but I can't find anything) I don't know how to retrieve the raw Json (or better, some kind of serialization) that comes in the request.

I see:

  • actionContext.Request.Content

But all methods are exposed as async. I don't know who this fits with passing the generate model to the controller method...

Thanks a lot!

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc