How can I properly overload a WebAPI 2 controller with multiple collection parameters?

I am trying to create my WebAPI controller with overloaded Get methods that will be selected based on parameters provided by the user. I can make this work properly in some cases, but when I have several collection parameters for a method, my controller can no longer select the correct route, even if I do not specify both collections.

For example, the following settings work:

[RoutePrefix("data/stock")]
public class StockDataController 
    : ApiController {

    private readonly IDataProvider<StockDataItem> _dataProvider;

    public StockDataController() {
        _dataProvider = new StockDataProvider();
    }

    [Route("")]
    public IEnumerable<StockDataItem> Get([FromUri] string[] symbols) {
        // Return current stock data for the provided symbols
    }

    [Route("")]
    public IEnumerable<StockDataItem> Get([FromUri] string[] symbols, DateTime time) {
        // Return stock data at a specific time for the provided symbols
    }

}

Selects method 1

GET http: // server / data / stock /? Symbols [] = GOOG & symbols [] = MSFT

Selects Method 2

GET http: // server / data / stock /? Symbols [] = GOOG & symbols [] = MSFT & time = 2015-01-01

As soon as I add the following overload then everything will break:

    [Route("")]
    public IEnumerable<dynamic> Get(
        [FromUri] string[] symbols, [FromUri] string[] fields) {
        // Return specified stock data fields for the specified symbols
    }

, 3:

GET http://server/data/stock/?symbols[]=GOOG&symbols[]=MSFT&fields[]=Price&fields[]=Volume

:

, : Get on type StockDataController Get on type StockDataController

? , ?

+4
1

REST.

nulleable: DateTime?

public IEnumerable<StockDataItem> Get([FromUri] string[] symbols, DateTime? time) {
    // Return stock data at a specific time for the provided symbols
}

:

GET http://server/data/stock/?symbols[]=GOOG&symbols[]=MSFT

GET http://server/data/stock/?symbols[]=GOOG&symbols[]=MSFT&time=2015-01-01

+2

All Articles