runtime: System.Text.Json settings not being applied
I am using Blazor WebAssembly 3.2 Preview 3 and System.Text.Json 5.0.0-preview.1.20120.5
configured as below:
services.AddMvc().AddJsonOptions(options =>
{
options.JsonSerializerOptions.WriteIndented = true;
options.JsonSerializerOptions.IgnoreNullValues = true;
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
options.JsonSerializerOptions.ReferenceHandling = ReferenceHandling.Preserve;
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});
This is the data I am requesting.
[DataContract(IsReference = true)]
public class State
{
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public List<Region> Regions { get; }
public State()
{
Name = string.Empty;
Regions = new List<Region>();
}
public State(string name)
{
Name = name;
Regions = new List<Region>();
}
}
This is the api method.
[HttpGet]
public async Task<ActionResult<List<State>>> GetReportsAsync()
{
return await context.States.Include(x => x.Regions).ToListAsync() is IList<State> states
? Ok(states)
: (ActionResult<List<State>>)NoContent();
}
This is the response.
[
{
"id": 1,
"name": "Espírito Santo",
"regions": []
}
]
When I do a request to an api method, it returns as if it had just ignored all of the configuration code. Property names aren’t camel cased and EF Core’s Include()
method isn’t working, which I am guessing it is related to reference handling at this point.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 18 (18 by maintainers)
No worries, reach out to us any time you think you have issues/suggestions on the
System.*
APIs. If you have blazor-related concerns, you can create an issue on https://github.com/dotnet/aspnetcore.if you have more questions feel free to comment or open a new issue.