0

I have built a Web API that 90% functions. Where I'm stuck is on how to accept jQuery style array encoded parameters using Web API. Here is my GET request:

GET /api/v1/Trades?take=3&skip=0&page=1&pageSize=3&sort%5B0%5D%5Bfield%5D=tradeName&sort%5B0%5D%5Bdir%5D=asc

Unencoded, the request looks like this:

GET /api/v1/Trades?take=3&skip=0&page=1&pageSize=3&sort[0][field‌​]=tradeName&sort[0][‌​dir]=asc

This style is consistent with how jQuery's $.param method serializes requests. How do I accept this as a parameter in Web API? I've tried the following:

[System.Web.Http.HttpGet]
[System.Web.Http.Route("api/v1/Trades")]
public IHttpActionResult GetTrades(int page = -1, int pageSize = -1, int skip = -1, int take = -1, string[,] sort = null)
{
    // Do stuff ...
}

In this instance, my method receives a 500 error with the following message:

Optional parameter 'sort' is not supported by 'FormatterParameterBinding'.

I've also tried this:

public IHttpActionResult GetTrades(string[,] sort, int page = -1, int pageSize = -1, int skip = -1, int take = -1)
{
    // Do stuff ...
}

Using this, my method gets called, but sort is always null. I've also tried it more generic, like this:

public IHttpActionResult GetTrades(object sort, int page = -1, int pageSize = -1, int skip = -1, int take = -1)
{
    // Do stuff ...
}

Again, my method gets called, but sort is always null.

Any suggestions would be helpful!

4
  • 1
    Possible duplicate of Multidimensional Array to MVC Controller Commented Dec 15, 2017 at 1:07
  • 1
    @CaiusJard - This is not the same. I am not trying to pass in a multidimensional array, nor am I trying to pass it into an MVC controller. I am trying to pass in a jQuery style array, which is different. The solution in the duplicate you posted does not work for me. Also, I'm not trying to pass it into an MVC controller. I'm trying to pass it into a Web API Controller. Commented Dec 15, 2017 at 4:24
  • At your insistence that the solution offered for an MVC controller cannot be adapted to your situation, I have retracted my close vote. Still surprised that MS implemented a completely different parsing routine for MVC routing compared to Web API routing., but clearly you've given it a go and it didn't work out! Thanks for the feedback Commented Dec 15, 2017 at 7:40
  • @Div - Completely different question. The question you linked is about how to parse data from a Kendo Grid. I'm trying to figure out how to parse data from a jQuery .param method. Commented Dec 15, 2017 at 15:37

1 Answer 1

0

If you are sending This many parameters then it is good practice to use POST method, in that you can send data in Object.

Still if you want to use GET method then use

[System.Web.Http.HttpGet]
[System.Web.Http.Route("api/v1/Trades")]
public IHttpActionResult GetTrades(int page = -1, int pageSize = -1, int skip = -1, int take = -1, string sort = "")
{
    // Do stuff ...
}

AND doing API call

GET /api/v1/Trades?take=3&skip=0&page=1&pageSize=3&sort=tradeName asc,Col2 desc

After getting call in GetTrades method split sort string data by comma(,) and then by space( ). So you will get column names and sort order.

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

1 Comment

This solution doesn't work. sort always returns an empty string. Also, about your comment to use a POST method, if you are creating a RESTful API and you are just retrieving data (not modifying it), isn't it a standard practice to make it a GET method?

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.