发布的数据asp.net的Web API [英] Posting data to asp.net Web API

查看:117
本文介绍了发布的数据asp.net的Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出新的ASP.NET Web API。

I'm trying to figure out the new ASP.NET Web API.

到目前为止,我已经能够创建此方法的签名,并连接到它只是罚款,并得到有效的回应...

So far I've been able to create this method signature and connect to it just fine and get a valid response...

[HttpPost]
public HttpResponseMessage CreateAccount()

我能请求发送给此方法与小提琴手,并已验证其收到了请求。

I am able to send a request to this method with fiddler and have verified that it is receiving the request.

然而,当我试图通过数据是,当我遇到一个问题。

However, when I try to pass data is when I am running into a problem.

我想的第一件事就是...

The first thing I tried was...

[HttpPost]
public HttpResponseMessage CreateAccount([FromBody]string email, [FromBody]string password)

和我键入

电子邮件:XYZ,密码:ABC

email:xyz,password:abc

成小提琴手请求的主体。当我这样做,我得到一个500错误,指出

into the body of the request in fiddler. When I do this I get a 500 error stating

无法绑定多个参数(电子邮件和密码),以请求的内容。

'Can't bind multiple parameters ('email' and 'password') to the request's content.'

我也试过这是一个方法的签名...

I have also tried this as a method signature...

[HttpPost]
public HttpResponseMessage CreateAccount([FromBody]UserAccountRequestData data)

与UserAccountRequestData是一个简单的POCO

with the UserAccountRequestData being a simple POCO

public class UserAccountRequestData
{
    public string Email { get; set; }
    public string Password { get; set; }
}

和我把

{电子邮件:XYZ,密码:ABC}

{Email:xyz,Password:abc}

数据:{电子邮件:XYZ,密码:ABC}

data:{Email:xyz,Password:abc}

入请求的主体。在这两种情况下,试图填充POCO我能够在调试时到达的方法,但数据对象总是空。

into the body of the request. In both cases trying to populate the POCO I am able to reach the method while debugging, but the data object is always null.

我需要了解如何创建接受API方法均强类型波苏斯和其他接受多个原始类型,如字符串和整数。

I need to understand how to create API methods that accept both strongly typed POCOs and others that accept multiple primitive types like strings and ints.

感谢

推荐答案

您需要设置内容类型应用程序/ JSON ,然后提供有效的JSON。

You need to set the Content-Type header to application/json and then provide valid JSON.

{"Email":"xyz","Password":"abc"}

这篇关于发布的数据asp.net的Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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