Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3630

How to configure JSON deserialization options in .NET minimal API project

$
0
0

I have a .NET minimal API project, as described here. One of the endpoints accepts JSON in the request body, and this is deserialized automatically. The code looks something like this:

app.MapPost("/myendpoint", async (MyInputFromBody input) => { ... });

This works in general, but I would like to specify options for deserialization. For instance, the JSON has some date fields such as "myDate": "2021-04-01T06:00:00", which can be converted to a DateTime, but an empty date value "myDate": "" throws an exception (System.FormatException: The JSON value is not in a supported DateTime format.).

I found a suggestion to write my own DateTime converter and use this:

builder.Services.AddControllers()    .AddJsonOptions(options => options.JsonSerializerOptions.Converters.Add(new JsonDateTimeConverter()));

but this doesn't seem to do anything. A breakpoint in my converter is never hit, and I get the same exception as before.

Is there a way to specify JSON options, or should I use a string parameter, as in [FromBody] string body, and parse that myself?


Viewing all articles
Browse latest Browse all 3630

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>