简单的发布到 Web Api [英] Simple post to Web Api
问题描述
我正在尝试获取使用 Web api 的发布请求.以下是我的 api 控制器.
I'm trying to get a post request to work with the web api. Following is my api controller.
public class WebsController : ApiController
{
[HttpPost]
public void PostOne(string id)
{
}
[HttpPost]
public void PostTwo(Temp id)
{
}
}
我已更改 webapi 路由以将操作考虑在内.Temp 模型看起来像这样.
I have altered the webapi route to take the action into account. the Temp model look something like this.
public class Temp
{
public string Id { get; set; }
}
我的观点是这样的
@using (Ajax.BeginForm(new AjaxOptions
{
Url = "/api/webs/postone",
HttpMethod = "post"
}))
{
<input name="id" id="id" value="2" />
<input type="submit" value="submit" />
}
除非我像这样将 [FromBody] 属性放在参数前面,否则上面的代码对 postone 根本不起作用.
the above code does not work at all with the postone unless I put the [FromBody] attribute in front of the parameter like this.
[HttpPost]
public void PostOne([FromBody]string id)
{
}
然后它点击了动作,但 id 仍然为空.它不会填充文本框中的值.
then it hits the action, but the id is still null. It doesn't get populated with the value in the textbox.
但是,如果我将 Ajax.BeginForm
的 Url
更改为 posttwo
这采用模型 Temp
,它运行良好并且 Id
字段在文本框中获得正确的值.
But, if I change the Url
of the Ajax.BeginForm
to posttwo
which take the model Temp
, it works nicely and the Id
field gets the proper value in the textbox.
任何人都可以向我解释发生这种情况的原因,以及我如何将一个简单的值发布到 Web api 操作中?我的意思是,为什么它可以绑定复杂类型而不能绑定简单类型.
can anyone please explain me the reason for this to happen and how I can post a simple value to a web api action? I mean, why can it bind a complex type but not a simple type.
推荐答案
我问这个问题已经有一段时间了.现在理解的更清楚了,我准备放一个更完整的答案来帮助别人.
It's been quite sometime since I asked this question. Now I understand it more clearly, I'm going to put a more complete answer to help others.
在 Web API 中,记住参数绑定是如何发生的非常简单.
In Web API, it's very simple to remember how parameter binding is happening.
- 如果您
POST
简单类型,Web API 会尝试从 URL 绑定它 如果你
POST
复杂类型,Web API 会尝试从请求(这使用media-type
格式化程序).
- if you
POST
simple types, Web API tries to bind it from the URL if you
POST
complex type, Web API tries to bind it from the body of the request (this uses amedia-type
formatter).
如果您想从 URL 绑定复杂类型,您将在操作参数中使用 [FromUri]
.这样做的限制取决于您的数据将保留多长时间以及它是否超过 url 字符限制.
If you want to bind a complex type from the URL, you'll use [FromUri]
in your action parameter. The limitation of this is down to how long your data going to be and if it exceeds the url character limit.
public IHttpActionResult Put([FromUri] ViewModel data) { ... }
如果您想从请求正文中绑定一个简单类型,您将在操作参数中使用 [FromBody].
If you want to bind a simple type from the request body, you'll use [FromBody] in your action parameter.
public IHttpActionResult Put([FromBody] string name) { ... }
作为旁注,假设您正在发出一个 PUT
请求(只是一个字符串)来更新某些内容.如果您决定不将其附加到 URL 并作为模型中只有一个属性的复杂类型传递,那么 jQuery ajax 中的 data
参数将如下所示.您传递给数据参数的对象只有一个属性名称为空的属性.
as a side note, say you are making a PUT
request (just a string) to update something. If you decide not to append it to the URL and pass as a complex type with just one property in the model, then the data
parameter in jQuery ajax will look something like below. The object you pass to data parameter has only one property with empty property name.
var myName = 'ABC';
$.ajax({url:.., data: {'': myName}});
您的 web api 操作将如下所示.
and your web api action will look something like below.
public IHttpActionResult Put([FromBody] string name){ ... }
这个asp.net 页面解释了这一切.http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api
This asp.net page explains it all. http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api
这篇关于简单的发布到 Web Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!