Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use FromQuery Attribute get a complex object?

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?

like image 870
丁祖巍 Avatar asked May 20 '16 09:05

丁祖巍


People also ask

What is FromQuery in .NET core?

[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.

How to pass a complex object as a parameter to action?

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.

How to fill the properties of a complex object in MySQL?

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.

Is it possible to use a complex type as a parameter?

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 complex objects as httpget parameters?

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.


1 Answers

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

like image 105
Roman Marusyk Avatar answered Sep 18 '22 13:09

Roman Marusyk