2

I'm trying to pass a complex object though the query string but for some reason its not working. I have a complex object that looks like this:

public class QueryOptions
{
    public QueryParameter[] Parameters = new QueryParameter[0];
}

And I've tried to sent it a few ways but nothing is working:

My webapi method looks like this:

[HttpGet]
[AllowAnonymous]
public async Task<TDTO[]> GetList([FromQuery] QueryOptions queryOptions)
{
    return await this._service.GetList(queryOptions);
}

I've tried with and with out the FromQuery Attribute neither are working. The url queries looks like this :

/api/users?Parameters[0].PropertyName=FirstName&Parameters[0].Value=GTitzy&Parameters[0].FilterCondition=0

I've also tried with the name of the object appened to the beginning. The request gets sent but the queryOptions always have no parameters.

How can I pass this complex object through the query string?

6
  • 1
    Don't. Use body and form.post(). Commented Aug 2, 2017 at 4:06
  • That is not going to work and GET should not be used to SEND complex objects. This appears to be an XY problem. What is the ultimate goal you are trying to achieve? Commented Aug 2, 2017 at 4:12
  • @Nkosi I'm trying to allow dynamic filtering on basic properties, I'm trying to pass a query options dto to filter my lists. I wanted to use the get so you can maintain the URL when passing it to a user Commented Aug 2, 2017 at 4:21
  • If I make it a post thought users won't be able to pass the URL around and I didn't feel like using odata, should I write a custom model binder or do you have anything in mind Commented Aug 2, 2017 at 4:24
  • @johnny5 Custom model binder would be best given the complexity of the object in question Commented Aug 2, 2017 at 4:26

1 Answer 1

2

Assuming

public class QueryParameter {
    public string PropertyName { get; set; }
    public string Value { get; set; }
    public string FilterCondition { get; set; }
}

You need to update your model to expose public properties for [FromQuery] to know what to bind to.

public class QueryOptions {
    public QueryParameter[] Parameters { get; set; }
}

You should also consider reading Model Binding: Customize model binding behavior with attributes

Sign up to request clarification or add additional context in comments.

3 Comments

I forgot to make my class use properties!
@johnny5 you should consider looking for a simpler way to pass your query options. IMO this is over complicating things
yeah I've been looking for a good way, but ending up doing it this way because, this is working generically for all my crud entities, Its applying universal filtering and and search api, and I'm crunched on my deadline

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.