✅ [JsonProprety] isn't being respected ASP.NET Core
Howdy! I've been having this issue that I can't get figure out.
For context this is a ASP.NET Core WebAPI
I have only one controller which is causing me issues and I can't quite figure out why.
This is the signature for it:
This is how
PostRequest
class looks like:
The issue is that [JsonProperty]
Attribute isn't being respected, whenever I sent a request like this:
The TimeStamps
doesn't get set to true
even though it has the stamps
attribute. If I set stamps
in curl to timestamps
it will work which leads me to believe that they're something wrong with the attributes. 🤔
I originally was using System.Text.Json
but switch to Newtonsoft.Json
thinking It might be a bug in System.Text.Json
but sadly nothing changed.7 Replies
Iirc default package is System.Text.Json (STJ) and uses a different attribute
You have to do something different in builder to use newtonsoft
Do you know what's the other Attribute called? 🤔
i think its JsonPropertyName
https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/customize-properties?pivots=dotnet-7-0
How to customize property names and values with System.Text.Json
Learn how to customize property names and values when serializing with System.Text.Json in .NET.
public class WeatherForecastWithPropertyNameAttribute
{
public DateTimeOffset Date { get; set; }
public int TemperatureCelsius { get; set; }
public string? Summary { get; set; }
[JsonPropertyName("Wind")]
public int WindSpeed { get; set; }
}
I was originally using
System.Text.Json
and JsonPropertyName
I still had same isuseChange all the JsonProperty to FromForm
https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-7.0#model-binding-simple-and-complex-types
Model Binding in ASP.NET Core
Learn how model binding in ASP.NET Core works and how to customize its behavior.
Thank you a lot! That worked