How to use FromQueryAttribute get a complex object?
[HttpGet]
public IActionResult Get([FromQuery] DataGridRequest request)
{
...
}
The DataGridRequest class like this:
public class DataGridRequest
{
public DataGridPager Pager { get; set; }
...
}
public class DataGridPager
{
public int Size { get; set; }
public int Index { get; set; }
}
How to write the uri?
[FromQuery] - Gets values from the query string. [FromRoute] - Gets values from route data. [FromForm] - Gets values from posted form fields. [FromBody] - Gets values from the request body.
Here, MyClass is the complex object we want to pass as a parameter to the action. Here the code of MyClass: Of course, we can place as many properties as we want in our class. Finally, to fill the properties of our complex object, we use query strings: With this, the properties of our object will be filled in the previous action.
Finally, to fill the properties of our complex object, we use query strings: With this, the properties of our object will be filled in the previous action. Something we should keep in mind, is that the validation rules are taken into account.
Additionally, the parameters generated aren't camelcased, and don't seem to honor JsonProperty either... basically, using a complex type as a parameter for API GET seems pretty well broken all around.
Is it possible to use a complex object as a parameter of an HttpGet action in ASP.NET Core? Yes, it is. We must use the FromQuery attribute.
You can try a Get request like this
controller?Size=1&Index=2
Also try to use [FromUri]
instead [FromQuery]
. The [FromUri]
attribute tries to bind the object properties to the query string properties by name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With