I'm making my first web api and I have the following controller:
// POST api/Recipes
[HttpPost]
public void Post([FromBody] RecipeModel newRecipe)
{
if (newRecipe != null)
{
using (var container = Db4oEmbedded.OpenFile(Db4oEmbedded.NewConfiguration(), DbPath))
try
{
var mayBeRecipe = (from RecipeModel recipe in container
where recipe.GetHashCode() == newRecipe.GetHashCode()
select recipe).FirstOrDefault();
if (mayBeRecipe == null)
container.Store(newRecipe);
}
catch (Exception e)
{
Debug.WriteLine(e);
}
}
}
To send newRecipe as a parameter I also have a type converter:
public class RecipeTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
// return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var newRecipe = JsonConvert.DeserializeObject<RecipeModel>(value as string);
return newRecipe;
}
}
The problem I have is that I try the api using Postman, sending it a RecipeModel in the body and the RecipeTypeConverter does not run (I put a breakpoint in the ConvertFrom method)
The json is the sgte:
{ "Name":"pure patata",
"Ingredients": [
{
"Name":"patata"
},
{
"Name":"sal"
}
],
"Yield":4,
"Steps":"mezclarlo todo"
}
Haber if you can lend me a hand because I've spent a lot of time investigating what happens.