Bind query parameters to a model in ASP.NET Core
For anyone that got here from search engine like me:
To make it work on asp.net core 3.1+
public async Task<IActionResult> Get([FromQuery] RequestDto request);
public class RequestDto
{
[FromQuery(Name = "otherName")]
public string Name { get; set; }
}
Will read json property otherName
into RequestDto.Name
so basically you have to use FromQuery
in 2 places.
Above answers are IMHO too complicated for such a simple thing already provided in asp.net framework.
Solution for .net core 2.1, 2.2, 3.0 and 3.1
Or without attributes you can do something like this which is cleaner I think (of course if the model properties are same as query parameters).
Meanwhile I use it in .net core 2.1, 2.2 and 3.0 preview & 3.1.
public async Task<IActionResult> Get([FromQuery]ReportQueryModel queryModel)
{
}
You need to add [FromQuery]
attribute to the model properties individually
public class Criteria
{
[FromQuery(Name = "first_name")]
public string FirstName { get; set; }
}