简单的POST到网页API [英] Simple post to Web Api

查看:170
本文介绍了简单的POST到网页API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个POST请求与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路线采取行动考虑在内。临时模型是这个样子。

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; }
}

我的观点是这个样子

my view look something like this

@using (Ajax.BeginForm(new AjaxOptions
{
    Url = "/api/webs/postone",
    HttpMethod = "post"
}))
{
    <input name="id" id="id" value="2" />
    <input type="submit" value="submit" />
}

以上code根本不与postone,除非我把[FromBody]属性在这样的参数前工作。

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 posttwo 内搭模型温度,它很好地工作和编号字段得到适当的值文本框。

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.

在网页API,这是非常简单的记住如何参数绑定正在发生的事情。

In Web API, it's very simple to remember how parameter binding is happening.


  • 如果您 POST 简单类型,网页API尝试从URL绑定

  • 如果您 POST 复杂类型的Web API试图从体绑定
    请求(该使用媒体类型格式)。

  • 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 a media-type formatter).

如果你想的复杂类型从URL绑定,你会在你的操作参数使用 [FromUri] 。这样的限制是到您的数据要去多久是,如果它超出了网址字符的限制。

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.

公共IHttpActionResult把([FromUri]视图模型数据){...}

如果您要绑定请求主体简单类型,你会在你的操作参数使用[FromBody]

If you want to bind a simple type from the request body, you'll use [FromBody] in your action parameter.

公共IHttpActionResult把([FromBody]字符串名称){...}

作为一个方面说明,说你正在一个 PUT 请求(只是一个字符串),以更新的东西。如果您决定不将其追加到URL和一个属性模型传递是一个复杂的类型,然后在jQuery的阿贾克斯数据参数将类似于下面。您传递给数据参数的对象都有空的属性名称只有一个属性。

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解释这一切。
<一href=\"http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api\">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

这篇关于简单的POST到网页API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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