C# 简单的发布到 Web Api

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19093603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:04:24  来源:igfitidea点击:

Simple post to Web Api

c#asp.net-mvc-4asp.net-web-apihttp-post

提问by Amila

I'm trying to get a post request to work with the web api. Following is my api controller.

我正在尝试获取使用 web api 的 post 请求。以下是我的 api 控制器。

public class WebsController : ApiController
{
    [HttpPost]
    public void PostOne(string id)
    {
    }

    [HttpPost]
    public void PostTwo(Temp id)
    {
    }
}

I have altered the webapi route to take the action into account. the Temp model look something like this.

我已经更改了 webapi 路由以考虑该操作。Temp 模型看起来像这样。

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" />
}

the above code does not work at all with the postone unless I put the [FromBody] attribute in front of the parameter like this.

除非我像这样将 [FromBody] 属性放在参数前面,否则上面的代码根本不适用于 postone。

[HttpPost]
public void PostOne([FromBody]string id)
{
}

then it hits the action, but the id is still null. It doesn't get populated with the value in the textbox.

然后它执行操作,但 id 仍然为空。它不会填充文本框中的值。

But, if I change the Urlof the Ajax.BeginFormto posttwowhich take the model Temp, it works nicely and the Idfield gets the proper value in the textbox.

但是,如果我改变Url了的Ajax.BeginFormposttwo内搭模型Temp,它很好地工作和Id领域在文本框中得到应有的价值。

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.

任何人都可以向我解释发生这种情况的原因,以及我如何向 Web api 操作发布一个简单的值?我的意思是,为什么它可以绑定复杂类型而不是简单类型。

采纳答案by Amila

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.

自从我问这个问题以来已经有一段时间了。现在我更清楚了,我准备放一个更完整的答案来帮助别人。

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

在 Web API 中,记住参数绑定是如何发生的非常简单。

  • if you POSTsimple types, Web API tries to bind it from the URL
  • if you POSTcomplex type, Web API tries to bind it from the body of the request (this uses a media-typeformatter).

  • 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) { ... }

  • 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) { ... }

  • 如果是POST简单类型,Web API 会尝试从 URL 绑定它
  • 如果您是POST复杂类型,Web API 会尝试从请求正文中绑定它(这使用media-type格式化程序)。

  • 如果您想从 URL 绑定复杂类型,您将[FromUri]在操作参数中使用。这样做的限制取决于您的数据将保存多长时间以及它是否超过 url 字符限制。

    public IHttpActionResult Put([FromUri] ViewModel data) { ... }

  • 如果您想从请求正文中绑定一个简单类型,您将在操作参数中使用 [FromBody]。

    public IHttpActionResult Put([FromBody] string name) { ... }

as a side note, say you are making a PUTrequest (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 dataparameter in jQuery ajax will look something like below. The object you pass to data parameter has only one property with empty property name.

作为旁注,假设您正在发出PUT请求(只是一个字符串)来更新某些内容。如果您决定不将其附加到 URL 并作为模型中只有一个属性的复杂类型传递,那么datajQuery ajax 中的参数将如下所示。您传递给 data 参数的对象只有一个属性名称为空的属性。

var myName = 'ABC';
$.ajax({url:.., data: {'': myName}});

and your web api action will look something like below.

并且您的 web api 操作将如下所示。

public IHttpActionResult Put([FromBody] string name){ ... }

This asp.net page explains it all. http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

这个asp.net 页面解释了这一切。 http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api