我有一个 F# WebAPI 项目,并且由于某种原因 Post 方法的参数始终设置为 null。
(姓名均已匿名)
这是 Controller :
type TestController() =
inherit ApiController()
member x.Get () =
getValues ()
member x.Post ([<FromBody>] values: Values) =
storeValues values
记录Values
的定义如下:
type TypeA = {
Id: string
....
}
....
type Values = {
AValues: list<TypeA>
BValues: list<TypeB>
CValues: list<TypeC>
}
在我看来,我对 API 进行了 POST 调用,如下所示:
return $.ajax({
type: "POST",
url: url,
data: JSON.stringify(params),
contentType: "application/json",
beforeSend: function (jqXHR) { jqXHR.setRequestHeader("X-HTTP-Method", type); }
});
数据的来源:
{
"AValues":[{"Id":"blahblah", ....}],
"BValues":[...],
"CValues":[...]
}
我更新了 JS 以使 data
计算为:
{
"Values":
{
"AValues":[{"Id":"blahblah", ....}],
"BValues":[...],
"CValues":[...]
}
}
现在调试器抛出一个 nullreference 异常并表示 Requests 为 null(断点位于我的记录定义上)。
问题是它确实命中了 Controller 中的 Post 方法,但 values
始终设置为 null。
最佳答案
我已将问题转发至Ryan Riley谁对 F# 和 Web API 都有期望,他说了以下内容:
You can't use records with the default model binder, even if you made the members mutable, because records don't have default constructors. F# 3.0 introduces a CLIMutableAttribute [1] that will allow records to be used this way.
[1] http://blogs.msdn.com/b/fsharpteam/archive/2012/07/19/more-about-fsharp-3.0-language-features.aspx
关于.net - 我的 WebAPI 后操作参数始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11785394/