AspNet 3.1 - Collides with another property : ThrowInvalidOperationException_SerializerPropertyNameConflict
you have to fix the classes, you have 2 choices
the common one
public class FooA
{
public string Property1 { set; get; }
public virtual string Property2 { set; get; }
}
public class FooB : FooA
{
public override string Property2 { set; get; }
public string Property3 { set; get; }
}
or if you want to have an access to 2 properties
public class FooA
{
public string Property1 { set; get; }
public string Property2 { set; get; }
}
public class FooB : FooA
{
public new string Property2 { set; get; }
public string Property3 { set; get; }
}
test
var json = @"{
""Property2"" : ""abc"",
""Property3"" : ""def""
}";
var jsonDeserialized=System.Text.Json.JsonSerializer.Deserialize<FooB>(json);
test reslut (in a json format)
{
"Property2": "abc",
"Property3": "def",
"Property1": null
}
but I recommend you to install Newtonsoft.Json serializer just config it in startup
using Newtonsoft.Json.Serialization;
services.AddControllersWithViews()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver());
or if you use just controllers
services.AddControllers()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver());