0

I'm building a set of web APIs that return data in JSON format. An external app performs an http (GET) request like this:

http://localhost:58807/api/items/search?qualifier=year&sort[0][field]=Year&sort[0][dir]=desc&sort[1][field]=Title&sort[1][dir]=asc

and this is the method which manage the http request:

[HttpGet]
[Route("search")]
public IHttpActionResult GetItems(string qualifier, IEnumerable<Dictionary<string, string>> sort)
{
     Does something;
}

This is the problem: while the qualifier parameter is correctly valued, the sort (square-bracket notation in query string) parameter is instead null. What's wrong?

Thanks in advance. Filippo

5
  • Have a look at Is array syntax using square brackets in URL query strings valid? - "square brackets may appear in a query string, but only if they are percent encoded." Commented Mar 27, 2020 at 23:03
  • Could you please clarify what is the problem? As it's currently written, it’s hard to tell exactly what you're asking. Do you mean that qualifier is ok, but sort is null? Do you use ASP.NET Core 2 or it is old ASP.NET? Commented Mar 27, 2020 at 23:26
  • Where is your [FromQuery] attribute? Also why enumerable of dictionary? Did you mean just a dictionary? Also don't forget [FromRoute] for the other one Commented Mar 28, 2020 at 8:15
  • @RomanMarusyk, sorry, you're right. Yes, the problem is exactly that: at the parameter qualifier was correctly assigned the value "year" but the sort parameter was null. I use "old" ASP.NET. The use of binding IEnumerable<Dictionary<string, string>> was suggested to me in an interesting article on parsing querystring containing arrays with square-bracket notation: link Commented Mar 28, 2020 at 21:37
  • @Node.JS There is no any route parameters except search. [FromQuery] attribute is not necessary if there is [ApiController] attribute. What is the problem with enumerable of dictionary? Commented Mar 28, 2020 at 21:38

1 Answer 1

0

In ASP.NET Web API 2 the binding does not work (using ApiController from namespace System.Web.Http).

In ASP.NET Core 3.1 Web API the binding does work (using ControllerBase from namespace Microsoft.AspNetCore.Mvc).

The difference is in the version.

One way of solution for Web API 2 would be to parse the query:

var sortParameters = Request.Query.Where(x => x.Key.StartsWith("sort"));
var sortFilters = new List<SortFilter>();
foreach (var sortParamter in sortParameters)
{
    sortFilters.Add(ParseSortParameter(sortParameter)); // TODO: implementation of ParseSortParameter
}


public class SortFilter
{
    public string FieldName { get; set; }
    public string Direction { get; set; }
}

Request is of type HttpRequest.

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

1 Comment

It depends on version of ASP.NET. The OP's code works well in ASP.NET Core

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.