gpt4 book ai didi

带有空数组的 ASP.NET MVC Int Array 参数默认为 {0}

转载 作者:行者123 更新时间:2023-12-01 04:15:22 25 4
gpt4 key购买 nike

我有一个 Controller Action ,如:

Public ActionResult MyAction(int[] stuff){}

我发出一个 JSON 请求,如:
$.getJSON(url, { stuff: [] })

当它到达 C# 时,它看起来像一个包含一个元素的数组,它是零(即就像我做了 int[] stuff = {0}; )。

这是 MVC 2 还是 .NET 4 的新功能?最近好像变了,但是我还没有找到确凿的证据。我怎样才能解决这个问题?这不可能是预期的行为,是吗?

最佳答案

我认为这是MVC中的一个错误:

// create a vpr with raw value and attempted value of empty string
ValueProviderResult r = new ValueProviderResult("", "", System.Globalization.CultureInfo.CurrentCulture);
// this next line returns {0}
r.ConvertTo(typeof(int[]));

如果我们查看函数 UnwrapPossibleArrayType 中的 ValueProviderResult.cs,我们会看到:
// case 2: destination type is array but source is single element, so wrap element in array + convert
object element = ConvertSimpleType(culture, value, destinationElementType);
IList converted = Array.CreateInstance(destinationElementType, 1);
converted[0] = element;
return converted;

它强制 converted[0]成为元素,并且 ConvertSimpleType 将 ""强制转换为 0。所以我要结束这个问题,除非有人有更多信息。

编辑:此外,这不在修订版 17270 中,因此如果您要列出从 MVC 1 更改为 MVC 2 的内容,这就是其中之一。

关于带有空数组的 ASP.NET MVC Int Array 参数默认为 {0},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4036329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com