如何在ASP.NET Core Web API中传递int数组 [英] How to pass an array of int in ASP.NET Core Web API
本文介绍了如何在ASP.NET Core Web API中传递int数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个Web API方法:
I have this Web API method:
[Route("api/[controller]")]
[ApiController]
public class SubjectsController : ControllerBase
{
[HttpGet("children")]
public IActionResult GetAllFromChildren([FromQuery]int[] childrenIds)
{
// omitted for brevity
}
}
我正在尝试通过Ajax传入查询字符串来调用它,但是我似乎无法使其正常工作.我的Ajax呼叫看起来像这样:
I'm trying to call this via Ajax passing in an query string but I can't seem to get it to work. My Ajax call looks like this:
$.ajax({
url: "/api/subjects/children?childrenIds=1&childrenIds=2&childrenIds=3",
method: "GET",
contentType: "application/json; charset=utf-8"
})
该方法被调用,但是不会填充int数组.我在做什么错了?
The method is called but it the int array does not get populated. What am I doing wrong?
推荐答案
尝试将Name
添加到[FromQuery]
,因此代码应如下所示:
Try add Name
to [FromQuery]
, so the code should look like this:
[Route("api/[controller]")]
[ApiController]
public class SubjectsController : ControllerBase
{
[HttpGet("children")]
public IActionResult GetAllFromChildren([FromQuery(Name="childrenIds")]int[] childrenIds)
{
// omitted for brevity
}
}
以及像这样的ajax网址:
and ajax url like this:
$.ajax({
url: "/api/subjects/children?childrenIds=1&childrenIds=2&childrenIds=3",
method: "GET",
contentType: "application/json; charset=utf-8"
})
这篇关于如何在ASP.NET Core Web API中传递int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文