I am trying to execute a custom implementation of MediaTypeFormatter
, but the bool CanReadType(Type type)
method is always called with type IKeyValueModel
, instead my type is MyProduct
.
In my API controller, I:
[HttpPost] public Task Save(MyProduct product)
I saw several examples ( example 1 , example 2 ), and I also tried the code:
protected override bool CanReadType(Type type) { if (type == typeof(IKeyValueModel)) return false; return true; }
But then I get "MissingMethodException: No parameterless constructor defined for this object" because my model type does not have a constructor without parameters. So it looks like this because CanReadType
returns false in IKeyValueModel
, the environment uses a different format. In fact, the OnReadFromStreamAsync
method never hits.
I want to be able to control how my models are deserialized, I want to get the real type, not IKeyValueModel
.
This already worked fine in model-bound MVC.
Greetings.
UPDATE 2012/05/29:
If I remove the default json formatter, it works:
public static void RegisterApis(HttpConfiguration config) { config.Formatters.Clear();
But then I would like to save both, how can I indicate which one I want to use?
Sincerely.
source share