FromBody 未绑定字符串参数 [英] FromBody not binding string parameter

查看:31
本文介绍了FromBody 未绑定字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于 ASP.NET 的问题MVC 4 RC Web API 参数绑定问题,但我正在尝试使用 [FromBody] 属性来解决它.

I have an issue similar to ASP.NET MVC 4 RC Web API Parameter Binding Issue, but I'm trying to solve it by using the [FromBody] attribute.

Fiddler 报告以下请求(不包括 User Agent String 等无关位)

Fiddler reports the following request (excluding irrelevant bits like User Agent String)

PUT http://localhost:82/api/account/shoppinglistitems HTTP/1.1
Host: localhost:82
Connection: keep-alive
Content-Length: 11
Origin: http://localhost:3000
Content-Type: application/x-www-form-urlencoded
Accept: application/json, text/javascript, */*; q=0.01

query=apple

我的控制器操作是

[HttpPut]
public ShoppingListItemWebModel CreateShoppingListItem([FromBody]string query) {
    // query is null
}

我可以将参数包装在复杂类型中,但这似乎是解决问题的一种技巧.或者我可以将查询放在 URI 中,但这不符合 API 其余部分的模式.可以这样做吗?如果是这样,我的错误在哪里?

I could wrap the parameter in a complex type, but that seems like a hack to fix the issue. Or I could put the query in the URI, but that doesn't fit the pattern of the rest of the API. Is it possible to do it this way? If so, where is my mistake?

推荐答案

更改您的要求

PUT http://localhost:82/api/account/shoppinglistitems HTTP/1.1
Host: localhost:82
Connection: keep-alive
Content-Length: 11
Origin: http://localhost:3000
Content-Type: application/x-www-form-urlencoded
Accept: application/json, text/javascript, */*; q=0.01

=apple

注意缺少查询"

这篇关于FromBody 未绑定字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆