Internal Server Error (500) with automatic deserialization of ASP.NET Web API

less than 1 minute read

phenomenon

When I call ASP.Net WebApi from a client, the parameters are automatically deserialized,
When I try to receive it in a certain data class, I get an error by blowing HttpStatusCode: 500.

***Controller.cs


        [HttpPost()]
        public async Task<ActionResult> Post(Photo photo)
        {
            return Ok();
        }

Photo.cs


    public class Photo{
        public string name{get;set;}
        public string src{get;set;}

        public Photo(string name, string src){
            this.name = name;
            this.src = src;
        }
    }

Data being sent


    { name: "aaa", src: "ccc"}

error contents


System.NotSupportedException: Deserialization of reference types without parameterless constructor is not supported. Type '*****.Models.Photo'
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException_DeserializeCreateObjectDelegateIsNull(Type invalidType)

Cause

An error occurred because there was no constructor with no arguments during deserialization.
That’s right. .. ..

Photo.cs


    public class Photo{
        public string name{get;set;}
        public string src{get;set;}

        public Photo(){}

        public Photo(string name, string src){
            this.name = name;
            this.src = src;
        }
    }