WebApi 使用 IEnumerable<int> 从 Uri 绑定到丰富模型
WebApi Bind from Uri to rich model with IEnumerable<int>
型号:
public class TestModel
{
public int Id { get; set; }
public int[] Clients { get; set; }
}
控制器:
public string Get([FromUri]TestModel model)
{
return "";
}
此请求按预期工作:localhost/get?id=5&clients=1&clients=2
--> Clients 被绑定为一个包含 2 个元素的数组 (1 & 2)
但是这个请求很奇怪:localhost/get?id=5&clients=
--> Clients 被绑定为一个包含 1 个元素 (0)
的数组
空数组或 null 都可以接受,但 int[] {0}
?
我怀疑这在技术上是无效的URL:
localhost/get?id=5&clients=
这可能会混淆模型活页夹。它试图将 某些东西 放入 clients
属性 中。因为 clients
是一个 int[]
而 int
的默认值是 0
,好吧,你去吧。
要保持数组为空,只需完全省略值:
localhost/get?id=5
型号:
public class TestModel
{
public int Id { get; set; }
public int[] Clients { get; set; }
}
控制器:
public string Get([FromUri]TestModel model)
{
return "";
}
此请求按预期工作:localhost/get?id=5&clients=1&clients=2
--> Clients 被绑定为一个包含 2 个元素的数组 (1 & 2)
但是这个请求很奇怪:localhost/get?id=5&clients=
--> Clients 被绑定为一个包含 1 个元素 (0)
空数组或 null 都可以接受,但 int[] {0}
?
我怀疑这在技术上是无效的URL:
localhost/get?id=5&clients=
这可能会混淆模型活页夹。它试图将 某些东西 放入 clients
属性 中。因为 clients
是一个 int[]
而 int
的默认值是 0
,好吧,你去吧。
要保持数组为空,只需完全省略值:
localhost/get?id=5