.net - 我的 WebAPI 后操作参数始终为空

标签 .net json f# asp.net-web-api

我有一个 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/

相关文章:

.net - SQL 查询的包装器

c# - 将 JSON 值写入 SQL 数据库

jquery - 禅宗编码 + jQuery

f# - 在 fable-elmish 中,如何在渲染 View 后触发命令?

c# - F# 如何处理 BCL 中数以千计的标准可变类型?

c# - DataContract/JsonSerializer '@' 附加到变量名称

C# atom 引用类型等效于 clojure atom

c# - 使用 FileShare.Delete 会导致 UnauthorizedAccessException 吗?

json - Play Framework : How to convert strings to numbers while validating JSON

javascript - 使用 javascript 从 JSON 文件读取时出现问题