0

a small testing code.

        //web api 2 controller 
   
        [HttpPost]
        [Route("api/test")]
        public IHttpActionResult Test(Person request)
        {
            var name = request.name;
            var age = request.age;

            return Json("ok");
        }
    }

    //model
    [Serializable] 
    public class Person 
    {
        public string name { get; set; }
        public int age { get; set; }
    }

and I just send json model in POST request

{
    "name": "mike", 
    "age": 12  
}

Why that code doesn't work (model came empty) if model Person has [Serializable] attribute? If I remove it starts working. The problem is I have to use those existing models and I don't understand why asp.net stop mapping them? Is JSON model which I send with POST request must be formated in another way?

Duke
  • 1,844
  • 2
  • 16
  • 26
  • "doesn't work" - it means the model came empty. – Duke Oct 08 '20 at 20:53
  • Although [this is for serializing the results of a Web API action method](https://stackoverflow.com/questions/29962044/using-serializable-attribute-on-model-in-webapi) it may still apply. – mason Oct 08 '20 at 23:39

0 Answers0