I'll try to explain my question on example below
if we have url such as this one:
localhost/api/person/5?FirstName=Adam&LastName=Smith&City=London
and try to get parameters
[HttpGet]
[Route("person/{someId}")]
public ActionResult Location(string someId, PersonRequest request)
Is it possible to try something like "someId" going to by binded to "/5" and everything after "?" is binded to PersonRequest?
public class PersonRequest
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? City { get; set; }
}
I've been trying to match it with [FromUri] etc. but I don't have clue how to do this. It only works if i list down all parameters in Location method.
CodePudding user response:
You can use FromQuery
Inline Attribute as per below:
[HttpGet]
[Route("person/{someId}")]
public ActionResult Location(string someId,[FromQuery] PersonRequest request)
{
// TODO
}
By the way, I believe a good practice is to only bind search terms, filters, etc. from the query string. If your use-case is to publish a rest API then binding from the body is the correct approach.
Consider checking Parameter Binding in ASP.NET Web API as it may help you to acknowledge deeper regarding the bindings.