Skip to content

Part1: Improve error messages for implicit and explicit FromBody attribute in Minimal API #35086

@rafikiassumani-msft

Description

@rafikiassumani-msft

Describe the bug

1. Forgetting to register a service with the DI container generates an incorrect error as the parameter is viewed as second [FromBody] attribute. Although from the user standpoint the second [FromBody] attribute is not explicit

app.MapPost("/createUser", ([FromBody] UserDTO userDTO, UserRepository userRepo) => {
    return $"user created  - {userDTO.LastName} - {userRepo.CreateUser(userDTO).LastName}";
 });

The user's intention was to inject the UserRepository as service to use.

Error message:

System.InvalidOperationException: 'Action cannot have more than one FromBody attribute.'

To Reproduce

Exceptions (if any)

See above explanation for exception being thrown.

Further technical details

.NET SDK (reflecting any global.json):
Version: 6.0.100-rc.1.21404.12
Commit: 78fb921136

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19043
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.100-rc.1.21404.12\

Host (useful for support):
Version: 6.0.0-rc.1.21403.13
Commit: c082af307d

.NET SDKs installed:
3.1.411 [C:\Program Files\dotnet\sdk]
5.0.301 [C:\Program Files\dotnet\sdk]
5.0.302 [C:\Program Files\dotnet\sdk]
6.0.100-preview.6.21355.2 [C:\Program Files\dotnet\sdk]
6.0.100-rc.1.21404.12 [C:\Program Files\dotnet\sdk]

Metadata

Metadata

Labels

area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-actionsController-like actions for endpoint routingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions