Reputation: 47
Can I convert this url:
http://localhost:4200/api/hoursRange=[0,12]
to int[] HoursRange
?
I'm using ASP.NET Core 2.2
I tried this code:
[HttpGet]
[Route("api")]
public IActionResult Get([FromQuery]int[] HoursRange)
{
// action
}
but it doesn't work.
Upvotes: 2
Views: 580
Reputation: 1076
Solution 1: The default model binder expects this url:
http://localhost:4200/api/hoursRange=1&hoursRange=2&hoursRange=3
Only work for this type of method:
[HttpGet]
[Route("api")]
public IActionResult Get([FromQuery]int[] HoursRange)
{
// action
}
Solution 2:
to do that but in different method but in asp.net mvc
Need to change method name like this
http://localhost:54119/api/MultipleData?ids=24041&ids=24117
in order to successfully bind to:
[HttpGet]
[Route("api")]
public ActionResult MultipleData(int[] ids)
{
...
}
Solution 3: For another verified solution check link
Solution 4: indexer is also one way to do but not an efficient way
http://localhost:4200/api/hoursRange[0]=24041&hoursRange[1]=24117&hoursRange[2]=24117
Informations
Please check for more customization customizing-query-string-parameter-binding-in-asp-net-core-mvc
Upvotes: 1
Reputation: 4376
Please take a look at the title Collections in Model Binding
It shows several ways in which this can be achieved.
A couple of the ways shown in the Microsoft documentation are:
hoursRange=0&hoursRange=12
Using an indexer
hoursRange[0]=0&hoursRange[1]=12
Upvotes: 1