Aspnetcore: Support multiple [FromRoute], [FromHeader], [FromBody], etc in a single object

Created on 23 Jan 2018  ·  3Comments  ·  Source: dotnet/aspnetcore

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?

area-mvc

Most helpful comment

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.

All 3 comments

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.

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

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.

Was this page helpful?
0 / 5 - 0 ratings