Aspnetcore.docs: Model Binding master issue

Created on 26 Dec 2019  Â·  5Comments  Â·  Source: dotnet/AspNetCore.Docs

This is the master issue for this doc. If you PR this doc and don't fix all the issues, open the issues you fix and close against that.

  • [ ] Remove 1.0, 1.1, 2.0 version selectors #15362

  • [x] ~How do I register custom converters globally rather than via annotations? #16315~

  • [x] ~PU: Is it possible to use more than one parameter in an action method with Json Post #12890~

Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Source - Docs.ms doc-enhancement

All 5 comments

Is it possible to use more than one parameter in an action method with Json Post #12890

I don't think this one is actionable. The topic states explicitly that only one parameter can be decorated with [FromBody] and the startup logic for MVC enforces this. Any attempt at a workaround would be much more involved than just creating a wrapper class.

How do I register custom converters globally rather than via annotations? #16315

This is covered in How to write custom converters for JSON serialization in .NET (linked from the topic). It's under the section Register a custom converter. I wouldn't repeat that in this topic, but a hint towards using AddJsonOptions in ConfigureServices to get access to JsonSerializerOptions might be useful.

So how do I make it so that they're registered globally for my application when doing model binding?

So how do I make it so that they're registered globally for my application when doing model binding?

For the example shown in the topic, it would look something like this:

services.AddControllers()
    .AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.Converters.Add(new ObjectIdConverter());
    });

I'm currently building a netcore 3.1 project and I accidentally left out [FromBody] in my POST action method and the model was bound correctly (it's quite a simple model). Is it no longer a requirement to use [FromBody]?

I tested this with it being the only parameter, and with a second parameter (id, that preceeds the model in the declaration) and both bound OK.

In past versions (2.x?) it hasn't worked, so perhaps worth clarifying the circumstances where it can be left off?

[FromBody] hasn't been required for some time, that's why it's not shown in the tutorials.

Was this page helpful?
0 / 5 - 0 ratings