mardi 28 avril 2020

How to pass "System.Object" type to the Controller through a FromQuery parameter

I have this method in my controller that needs to receive a List of filtering expressions, so I can filter a DynamoDB table but the Value to filter can be string, numeric, boolean, etc... so I used a "System.Object" typed property. But it seems the controller can't bind a FromQuery Parameter to a Type "System.Object" object.

        [HttpGet]
        public IActionResult Get(
            [FromRoute]string name,
            [FromQuery]FilterExpression[] filters
            )
        {
            return Ok(m_attributeService.GetByAttributeName(name, filters));
        }

The FilterExpression class

    public class FilterExpression
    {
        public FilterExpression(){}
        public FilterExpression(
            string field, 
            string comparisonOperator, 
            dynamic values
            )
        {
            Field = field;
            ComparisonOperator = comparisonOperator;
            Values = values;
        }

        public string Field { get;set; }
        public string ComparisonOperator { get;set; }
        public object Values { get;set; }
    }

But every time I do any request the Values Property is always null. I want to know a workaround solution to receive different data types on my method.




Aucun commentaire:

Enregistrer un commentaire