Angular 参数字典包含空值,请求正文中存在参数

Angular 参数字典包含空值,请求正文中存在参数,angular,asp.net-web-api,frombodyattribute,Angular,Asp.net Web Api,Frombodyattribute,我正在尝试将枚举发布到我的WebAPI中。请求正文包含我的参数,控制器有一个[FromBody]标记。问题是,即使参数在主体中,我也会得到一个空条目错误 我有以下api控制器方法: public ApiResponse Post([FromBody]Direction d) { ... } 其中Direction位于文件turtle.cs中的枚举中: { public enum Direction { N, S, E, W } public class Turtle

我正在尝试
枚举发布到我的WebAPI中。请求正文包含我的参数,控制器有一个
[FromBody]
标记。问题是,即使参数在主体中,我也会得到一个空条目错误

我有以下api控制器方法:

public ApiResponse Post([FromBody]Direction d)
{
    ...
}
其中
Direction
位于文件
turtle.cs
中的枚举中:

{
    public enum Direction { N, S, E, W }

    public class Turtle
    {
       ...
    }
}
我正试图使用以下命令来
POST
从角度向webapi控制器发送一个方向:

html

编辑 尝试使用字符串而不是int,运气不佳:


在这种情况下,您实际上只想将值发送回API,而不是对象

原因是,当API尝试绑定请求正文中的值is get时,它将尝试在
方向
枚举中查找名为
d
的属性。如果它没有找到它要找的东西,它只返回null

因为您只是传递一个枚举值,所以只需要将该值作为请求体包含在内。然后绑定将按预期工作

因此,与其将此作为帖子:

this.http.post<Object>(this.gameModelUrl, {'d': d}, { headers: this.headers })...
this.http.post(this.gameModelUrl,{'d':d},{headers:this.headers})。。。
你有这个:

this.http.post<Object>(this.gameModelUrl, d, { headers: this.headers })...
this.http.post(this.gameModelUrl,d,{headers:this.headers})。。。

尝试发送值而不是索引{N,S,E,W}恐怕这也不太可能-更新的问题。如果您只是将
d
作为主体传递,而不是
{d':d}
?有什么吗?@R.Richards就是这样!杰出的我将在几分钟后发布答案。
POST https://localhost:44332/api/tasks 400 ()
MessageDetail: "The parameters dictionary contains a null entry for parameter 'd' of non-nullable type 'TurtleChallenge.Models.Direction' for method 'Models.ApiResponse Post(TurtleChallenge.Models.Direction)' in 'TaskService.Controllers.TasksController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."
this.http.post<Object>(this.gameModelUrl, {'d': d}, { headers: this.headers })...
this.http.post<Object>(this.gameModelUrl, d, { headers: this.headers })...