Deserialization of interface types is not supported

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

Truyền trực tiếp chuỗi JSON đúng thì được là phải rồi. Vấn đề là ở POST, bạn lấy dữ liệu từ đâu? Dữ liệu là chuỗi văn bản, hay dạng nhị phân?

2 Likes


mình post lên json object luôn bạn

  1. Bạn cho mình xem đầy đủ thông báo lỗi.
  2. Bạn thử in ra chuỗi mà bạn nhận được trong POST xem nó có đúng chưa.
3 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?