0

I want to pass a collection of strings to an API endpoint:

[HttpGet]
public DashboardSectionViewModel GetDashboard(
    DateTime lowerBound,
    DateTime upperBound,
    [FromUri] List<string> excludedStores = null,
    [FromUri] List<string> excludedItems = null)
{
    //Code
}

Here is where I make the call:

$.ajax({
    url: App.Services.updateUrl + src.updateUrl,
    type: "GET",
    data: {
        lowerBound: self.slideFilter.lowerBoundDisplay(),
        upperBound: self.slideFilter.upperBoundDisplay(),
        excludedStores: self.storeFilter.excludedStores(),
        excludedItems: self.experienceFilter.excludedItems()
    }
});

It works just fine when the two collection items are not parameters and are not passed by the AJAX request. It didn't work when I initially used IEnumerable<string> and changing to List<string> did not work either. Do I need to set a custom route for this to work? Will the interpreter get DateTime but not IEnumerable<T>?

EDIT: Here is the URL being generated with empty collections passed:

(header)/api/ContentApi/GetDashboard?lowerBound=10-12&upperBound=10-23

Here is the URL being generated with some collection items passed:

(header)/api/ContentApi/GetDashboard?lowerBound=10-10&upperBound=10-23&excludedStores%5B%5D=hello&excludedStores%5B%5D=world&excludedExperiences%5B%5D=hello&excludedExperiences%5B%5D=world

I think this is a serialization issue, but I'm not certain.

3
  • Tried sticking the parameters into a class, and have only one object as a parameter to your API method? Commented Nov 13, 2014 at 22:53
  • I didn't think this would work since it isn't interpreting the collections correctly. After having tested it, it isn't interpreting the object either. Commented Nov 13, 2014 at 22:58
  • Instead of passing a list of strings, try to pass the value as a string and use a tokenizer inside the method to split the string into an enumerable of strings. Commented Nov 14, 2014 at 2:41

1 Answer 1

1

In order for web api to bind to your action parameters your query should look like this:

?lowerBound=10-12&upperBound=10-20&excludedStores=hello&excludedStores=world&excludedItems=hello&excludedItems=world

This will bind to the parameters in your action:

public DashboardSectionViewModel Get(DateTime lowerBound, DateTime upperBound, [FromUri]List<string> excludedStores = null, [FromUri]List<string> excludedItems = null)

To create your query you will have to invoke $.param() and then replace the url encoded [] characters. Like this:

var query = $.param({
    lowerBound: '10-12',
    upperBound: '10-20',
    excludedStores: ['hello', 'world'],
    excludedItems: ['hello', 'world']
}).replace(/%5B%5D/g, '')

$.ajax({
    url: App.Services.updateUrl + src.updateUrl + '?' + query,
    type: "GET"
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.