Skip to content
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

Support multiple [FromRoute], [FromHeader], [FromBody], etc in a single object #4869

Closed
snebjorn opened this issue Jan 23, 2018 · 3 comments
Closed
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Milestone

Comments

@snebjorn
Copy link

I'm trying to create an object with all the relevant data from a request bound.

Imagine an action like this

[HttpPost({id})]
public virtual async Task<IActionResult> Post(PostRequest model)

And the model could be like this

public class PostRequest
{
    [FromRoute]
    public int Id { get; set; }

    [FromHeader]
    public string SomeHeader { get; set; }

    [FromBody]
    public int Foo { get; set; }
    [FromBody]
    public string Bar { get; set; }
    [FromBody]
    public string Baz { get; set; }
}

Unfortunately multiple [FromBody] isn't allowed.

If it was allowed and worked as expected I could implement a generic CRUD controller like so

[HttpPost({id})]
public virtual async Task<IActionResult> Post(PostRequest model)
{
    var command = _mapper.Map<TCommand>(model); // AutoMapper
    await _mediator.Send(command); // MediatR
    // omitted for brevity
    return Created();
}

Obviously it wouldn't look exactly like that.
The idea is to remove all logic from the controller that isn't API/MVC related. So all the controller does is

  • validate the model
  • extract the wanted data from the request
  • send the relevant data of to a handler
  • return an appropriate http response

Now I can have one implementation for all my CRUD controller needs - and there was much rejoicing!

It doesn't need to be [FromBody] it could be another attribute. Like [FromBodyPart], [BindFromBody] or something completely different.

Would that be feasible?

@mkArtakMSFT
Copy link
Member

Thank you for your feature request. We'll consider this feature during the next release planning period and update the status of this issue accordingly.

@VladimirMakaev
Copy link

I thinks this would be a solution for my SO question that I still have unanswered here:
https://stackoverflow.com/questions/44261111/how-to-model-bind-path-segment-to-object-property

@aspnet-hello aspnet-hello transferred this issue from aspnet/Mvc Dec 13, 2018
@aspnet-hello aspnet-hello added this to the Backlog milestone Dec 13, 2018
@aspnet-hello aspnet-hello added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Dec 13, 2018
@pranavkm pranavkm added the c label Aug 21, 2019
@mkArtakMSFT
Copy link
Member

Thanks for contacting us.
After reevaluating this ask we've decided not to provide such a feature as this is not well aligned with the principles we try to follow in the framework.

@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

5 participants