C
C#16mo ago
Sarco

✅ [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:
[HttpPost]
public async Task<IActionResult> Post([FromForm] PostRequest request, [FromForm] IFormFile? file)
{
...
}
[HttpPost]
public async Task<IActionResult> Post([FromForm] PostRequest request, [FromForm] IFormFile? file)
{
...
}
This is how PostRequest class looks like:
using Newtonsoft.Json;

public class PostRequest
{
[JsonProperty("stamps")]
public bool TimeStamps { get; set; }

[JsonProperty("lang")]
public string Lang { get; set; } = "auto";

[JsonProperty("translate")]
public bool Translate { get; set; }

[JsonProperty("model")]
public string Model { get; set; } = "base";
}
using Newtonsoft.Json;

public class PostRequest
{
[JsonProperty("stamps")]
public bool TimeStamps { get; set; }

[JsonProperty("lang")]
public string Lang { get; set; } = "auto";

[JsonProperty("translate")]
public bool Translate { get; set; }

[JsonProperty("model")]
public string Model { get; set; } = "base";
}
The issue is that [JsonProperty] Attribute isn't being respected, whenever I sent a request like this:
curl -X POST \
-H "Content-Type: multipart/form-data" \
-F "file=@/path/to/file.mp3" \
-F "lang=en" \
-F 'stamps=true' \
-F "model=base" \
-F "translate=true" \
<insert url>
curl -X POST \
-H "Content-Type: multipart/form-data" \
-F "file=@/path/to/file.mp3" \
-F "lang=en" \
-F 'stamps=true' \
-F "model=base" \
-F "translate=true" \
<insert url>
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
Mayor McCheese
Mayor McCheese16mo ago
Iirc default package is System.Text.Json (STJ) and uses a different attribute You have to do something different in builder to use newtonsoft
Sarco
Sarco16mo ago
Do you know what's the other Attribute called? 🤔
ACiDCA7
ACiDCA716mo ago
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.
Mayor McCheese
Mayor McCheese16mo ago
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; } }
Sarco
Sarco16mo ago
I was originally using System.Text.Json and JsonPropertyName I still had same isuse
Wz
Wz16mo ago
Model Binding in ASP.NET Core
Learn how model binding in ASP.NET Core works and how to customize its behavior.
Sarco
Sarco16mo ago
Thank you a lot! That worked