public class Model
{
public Model()
{
}
public string Name { get; set; }
public int Id { get; set; }
[ModelBinder(typeof(ConcreteConverter<ModelTest>))]
public IModel iModel { get; set; }
}
public class ModelTest : IModel
{
public int Id1 { get; set; }
public string Name1 { get; set; }
}
public interface IModel
{
public int Id1 { get; set; }
public string Name1 { get; set; }
}
public class ConcreteConverter<T> : JsonConverter
{
public ConcreteConverter()
{
}
public override bool CanConvert(Type objectType) => true;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return serializer.Deserialize<T>(reader);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
}
[HttpGet]
public void Get()
{
var json = @"{'name':'test','id':1,'iModel':{'id1':1,'name1':'name1'}}";
var test = JsonConvert.DeserializeObject<Model>(json);
}
[HttpPost]
public IActionResult Post( Model model)
{
return StatusCode(200);
}
Mình có model thế này, với trường hợp hàm Get tức mình truyền trực tiếp chuỗi json vào thì DeserializeObject chạy được
Còn với hàm Post thì model post lên bị báo lỗi “Deserialization of interface types is not supported” không biết có bạn nào từng gặp vấn đề này chưa có thể help mình với
Thank