Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rafikiassumani-msft opened this issue Aug 6, 2021 · 0 comments · Fixed by #35188
Assignees
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Milestone

Comments

@rafikiassumani-msft
Copy link
Contributor

rafikiassumani-msft commented Aug 6, 2021

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]

@rafikiassumani-msft rafikiassumani-msft added feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Aug 6, 2021
@rafikiassumani-msft rafikiassumani-msft added this to the 6.0-rc1 milestone Aug 6, 2021
@rafikiassumani-msft rafikiassumani-msft changed the title Improve error messages for implicit and Explicit FromBody attribute in Minimal API Improve error messages for implicit and explicit FromBody attribute in Minimal API Aug 6, 2021
@rafikiassumani-msft rafikiassumani-msft self-assigned this Aug 6, 2021
@rafikiassumani-msft rafikiassumani-msft changed the title Improve error messages for implicit and explicit FromBody attribute in Minimal API Part1: Improve error messages for implicit and explicit FromBody attribute in Minimal API Aug 17, 2021
rafikiassumani-msft added a commit that referenced this issue Aug 17, 2021
…n Minimal API #35086 (#35188)

* Improve error messages for implicit and explicit FromBody attribute in Minimal API

* Add error message for mapget when user forget to register service

* add suggested changes

* replace null with typeof(Tvalue)

* multiple frombody

* resolve more merge issues

* address pr suggestions

* address more PR comments

* clean up

* reset HttpRequestJsonExtensions.cs
@ghost ghost locked as resolved and limited conversation to collaborators Sep 16, 2021
@amcasey amcasey added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
None yet
2 participants