POST的WebAPI作品,未经[FromBody] [英] WebApi POST works without [FromBody]?

查看:502
本文介绍了POST的WebAPI作品,未经[FromBody]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的控制器动作:

I have this controller action :

 [HttpPost]
 [ActionName("aaa")]
 public HttpResponseMessage aaa(Z z) //notice  - no [FromBody]
 {
    return Request.CreateResponse(HttpStatusCode.OK, 1);  
 }

其中,以Z 是:

public class Z
 {
   public string a { get; set; }
 }

但是,当我通过的提琴手发布:​​

But when I post via fiddler :

POST http://localhost:11485/api/profiles/aaa HTTP/1.1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Host: localhost:11485
Content-Length: 3
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,he;q=0.6
Cache-Control: no-cache
Connection: keep-alive

a=1

其实,我可以看到它正在工作:

I can actually see it is working :

问:

如果是这样,它是如何工作的,而不[FromBody]属性?而且我还需要/不写这个属性?

If so , how does it working without the [FromBody] attribute ? and do I still need /not write this attribute ?

此外,什么就是不写此属性的场景 - 会产生问题。

Also , what is the scenario where not writing this attribute - will cause problems ?

推荐答案

绑定是MVC和Web API不同。默认情况下,既然你指定的ASP.NET Web API绑定从请求消息的身体和简单类型的URI,查询字符串等复杂类型以Z ,这是一个类(复杂型),其植入来自身体的动作方法参数,您无需指定 [FromBody] 。在另一方面,如果你的要求的http://本地主机:11485 / API /型材/ AAA A = 1 没有身体,也不会绑定到你的复杂自动参数。在这种情况下,你需要指定 [FromUri] 是这样的:公开的Htt presponseMessage AAA([FromUri] Z Z)

Binding is different for MVC and Web API. By default, ASP.NET Web API binds complex types from the request message body and simple types from URI, query string, etc. Since you specified Z, which is a class (complex type), it populates the action method parameter from body without you having to specify [FromBody]. On the other hand, if your request is http://localhost:11485/api/profiles/aaa?a=1 without a body, it will NOT bind to your complex parameter automatically. In that case, you will need to specify [FromUri] like this: public HttpResponseMessage aaa([FromUri]Z z).

在另一方面,说你的操作方法是公开的Htt presponseMessage AAA(字符串)。我们现在有字符串,这是一个简单的类型。在这种情况下,的http://本地主机:11485 / API /型材/ AAA A = 1 没有消息体将让网页API绑定正确的参数,您无需?指定 [FromUri] 。现在,如果你想从身体绑定在这种情况下,你需要指定公开的Htt presponseMessage AAA([FromBody]字符串)。当然,对于这个机构必须是 = 1 应用程序/ x-WWW的形式urlen codeD ,没有的键名 A

On the other hand, say your action method is public HttpResponseMessage aaa(string a). We now have string, which is a simple type. In this case, http://localhost:11485/api/profiles/aaa?a=1 without a message body will let Web API bind the parameter correctly without you having to specify [FromUri]. Now, if you want to bind from body in this case, you will need to specify public HttpResponseMessage aaa([FromBody]string a). Of course, for this body must be =1, for application/x-www-form-urlencoded, without the key name of a.

底线 - 你的参数(简单类型或复杂类型)决定了网页API结合。为了让它从默认的行为的工作方式不同,需要通过告诉 FromUri FromBody

Bottom line - your parameter (simple type or complex type) determines how Web API binds. To make it work differently from the default behavior, you need to tell via FromUri or FromBody.

PS。无论我上面提到的用于保存好老的ASP.NET Web API(含2个)纯粹是不错的。在ASP.NET 5.0 a.k.a ASP.NET vNext或ASP.NET MVC 6.0,MVC和Web API已经统一。为了使它结合体,从一个复杂的类型,必须指定 [FromBody]

PS. Whatever I mentioned above holds good purely for the good old ASP.NET Web API (including 2). In ASP.NET 5.0 a.k.a ASP.NET vNext or ASP.NET MVC 6.0, MVC and Web API have been unified. In order for it to bind a complex type from body, you must specify [FromBody].

这篇关于POST的WebAPI作品,未经[FromBody]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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