I'm trying to figure out the new ASP.NET Web API.
我正在试图弄清楚新的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.
我能够使用fiddler向此方法发送请求,并已验证它正在接收请求。
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)
And I type
我输入
email:xyz,password:abc
电子邮件:XYZ,密码:ABC
into the body of the request in fiddler. When I do this I get a 500 error stating
进入提琴手请求的主体。当我这样做时,我得到一个500错误说明
'Can't bind multiple parameters ('email' and 'password') to the request's content.'
'无法将多个参数('email'和'password')绑定到请求的内容。
I have also tried this as a method signature...
我也试过这个方法签名...
[HttpPost]
public HttpResponseMessage CreateAccount([FromBody]UserAccountRequestData data)
with the UserAccountRequestData being a simple POCO
UserAccountRequestData是一个简单的POCO
public class UserAccountRequestData
{
public string Email { get; set; }
public string Password { get; set; }
}
And I put
我说
{Email:xyz,Password:abc}
{电子邮件:XYZ,密码:ABC}
or
要么
data:{Email:xyz,Password:abc}
数据:{电子邮件:XYZ,密码:ABC}
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.
进入请求的正文。在尝试填充POCO的两种情况下,我都能够在调试时访问该方法,但数据对象始终为null。
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.
我需要了解如何创建接受强类型POCO和其他接受多种基本类型(如字符串和整数)的API方法。
Thanks
谢谢
1 个解决方案
#1
17
You need to set the Content-Type
header to application/json
and then provide valid JSON.
您需要将Content-Type标头设置为application / json,然后提供有效的JSON。
{"Email":"xyz","Password":"abc"}
#1
17
You need to set the Content-Type
header to application/json
and then provide valid JSON.
您需要将Content-Type标头设置为application / json,然后提供有效的JSON。
{"Email":"xyz","Password":"abc"}