的WebAPI - 使用[FromBody]属性和POST方法时动作paramer为空 [英] WebApi - action paramer is null when using [FromBody] attribute and POST method

查看:10507
本文介绍了的WebAPI - 使用[FromBody]属性和POST方法时动作paramer为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的控制器和我想不通,为什么名称参数为空

I have this controller and I can't figure out, why name parameter is null

public class DeviceController : ApiController
{
    [HttpPost]
    public void Select([FromBody]string name)
    {
        //problem: name is always null
    }
}

这是我的路由映射:

here is my route mapping:

public void Configuration(IAppBuilder appBuilder)
{
    HttpConfiguration config = new HttpConfiguration();
    config.Routes.MapHttpRoute(
        name: "ActionApi",
        routeTemplate: "api/{controller}/{action}"
    );

    appBuilder.UseWebApi(config);
}

和这里是我的要求:

POST http://localhost:9000/api/device/Select HTTP/1.2
User-Agent: Fiddler
Host: localhost:9000
Content-Length: 16
Content-Type: application/json

{'name':'hello'}

我也试图改变身体纯字符串:你好

POST http://localhost:9000/api/device/Select HTTP/1.2
User-Agent: Fiddler
Host: localhost:9000
Content-Length: 5
Content-Type: application/json

hello

请求返回204是确定的,但参数是永远不会映射到发布的值。

The request returns 204 which is ok, but the parameter is never mapped to a posted value.

*我使用的是自托管owin服务。

*I'm using self hosted owin service.

推荐答案

在第一个例子中你使用的是复杂的对象 {'名':'你好'} 时在 [FromBody] 属性告诉粘结剂寻找一个简单的类型。

In the first example You were using a complex object {'name':'hello'} when the [FromBody] attribute told the binder to look for a simple type.

在第二个例子中的身体您提供的价值不能PTED作为一个简单的类型间$ P $,因为它缺少引号你好

In the second example your provided value in the body could not be interpreted as a simple type as it was missing quotation marks "hello"

使用[FromBody]

要强制网络API来读取请求主体一个简单类型时,[FromBody]属性添加到参数:

To force Web API to read a simple type from the request body, add the [FromBody] attribute to the parameter:

public HttpResponseMessage Post([FromBody] string name) { ... }

在这个例子中,网页API将使用一个媒体类型格式化以从请求主体读取名称的值。下面是一个例子客户端请求。

In this example, Web API will use a media-type formatter to read the value of name from the request body. Here is an example client request.

POST http://localhost:5076/api/values HTTP/1.1
User-Agent: Fiddler
Host: localhost:5076
Content-Type: application/json
Content-Length: 7

"Alice"

当一个参数有[FromBody],网络API使用Content-Type头,选择格式化。在本实施例中,内容类型是应用/ JSON和请求主体是一个原始JSON字符串(未JSON对象)。

When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/json" and the request body is a raw JSON string (not a JSON object).

目前最一个参数是允许从邮件正文阅读。所以这是行不通的:

At most one parameter is allowed to read from the message body. So this will not work:

// Caution: Will not work!    
public HttpResponseMessage Post([FromBody] int id, [FromBody] string name) { ... }

这样做的原因的规则是,请求体可能被存储在只能使用一次读的非缓冲流

The reason for this rule is that the request body might be stored in a non-buffered stream that can only be read once.

来源:<一个href=\"http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api\"相对=nofollow>参数中的ASP.NET Web API 绑定

这篇关于的WebAPI - 使用[FromBody]属性和POST方法时动作paramer为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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