I have an API app with .net6, with following configuration :
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString;
...
});
Also tried this :
builder.Services.AddControllers();
builder.Services.Configure<JsonOptions>(options =>
{
StringEnumConverter(allowIntegerValues: true));
options.JsonSerializerOptions.NumberHandling =JsonNumberHandling.AllowReadingFromString;
options.JsonSerializerOptions.PropertyNamingPolicy = null;
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
options.JsonSerializerOptions.Converters.Add(new Json
});
How ever when a model which has a string property, passed by post to an action, but that property set with number, the model is always null :
Request:
{ "AA": 3, }
Mode:
public class SampleModel { public string AA { get; set; } }
However if I use this convertor and clean the 'NumberHandling' from configuration, it works.
Is there any configuration I am missing ?