question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support multiple FromRoute and FromQuery per Object

See original GitHub issue

Using ASP.NET Core 2.2 I have the following ApiController action:

[ApiController]
public class PostController : Controller {

    [HttpGet("posts/{postId:int:min(1)}")]
    public async Task<IActionResult> GetByPostId([FromQuery]GetByPostIdRequest request {
    
    }
}

Where GetByPostIdRequest is the following:

public class GetByPostIdRequest {
    [FromRoute]
    public Int32 PostId { get; set; }
    public String LanguageCode { get; set; }
    public IncludeExpression Include { get; set; }
}

The only way all parameters get values are:

  1. Have FromQuery in action so I there is no Unsupported Media Type error
  2. Have FromRoute inside the Request class to bind the PostId.

Isn’t there another way to do this?

Possible Solution A suggestion could be to allow multiple attributes for one model ([FromRoute, FromQuery]):

    [HttpGet("posts/{postId:int:min(1)}")]
    public async Task<IActionResult> GetByPostId([FromRoute, FromQuery]GetByPostIdRequest request) {
    }

}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mateusvbfcommented, May 3, 2019

Try something like that:

[HttpGet("posts/{postId:int:min(1)}")]
public async Task<IActionResult> GetByPostId([FromRoute] int postId, [FromQuery]GetByPostIdRequest request) 
{
      request.PostId = postId;
}
0reactions
msftbot[bot]commented, Dec 7, 2019

Thank you for contacting us. Due to no activity on this issue we’re closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn’t been addressed yet, please file a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How might I simultaneously bind FromQuery and ...
I am needing to simultaneously support a query-parameter based route ( /api/models?id=1 ) and a route based one ( /api/models/1 ) while still ......
Read more >
How to Pass Multiple Parameters GET Method ASP.NET ...
You have multiple options for passing multiple parameters to a GET method: FromRouteAttribute, FromQuery and Model Binding.
Read more >
Model Binding in ASP.NET Core
Learn how model binding in ASP.NET Core works and how to customize its behavior.
Read more >
Create web APIs with ASP.NET Core
When more than one route matches an action parameter, any route value is considered [FromRoute] . [FromQuery] is inferred for any other action ......
Read more >
ASP.NET Core Web API Attributes
First the FromRoute attribute tells the framework to look in the route (URL) for an "id" value and provide that as the id...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found