[英]Passing Json to the Web Service
I'm trying to push json data to the webservice; 我正在尝试将json数据推送到Web服务;
Here is my sample data; 这是我的样本数据;
{
"data" : {
"username" : "demo",
"password" : "demo1082*098/*42a",
"LoginToken" : "AAFF540EC55DASEFBE7E3D8404AED31F6DD30CA2BFCE2433B9475E696GG38730"
}
}
Here is my webservice; 这是我的网络服务;
public string Post([FromBody]string data)
List<Login> datas = JsonConvert.DeserializeObject<List<Login>>(data);
Error; 错误; An exception of type 'System.ArgumentNullException' occurred in Newtonsoft.Json.dll but was not handled in user code Newtonsoft.Json.dll中发生类型为'System.ArgumentNullException'的异常,但未在用户代码中处理
Data appears as NULL 数据显示为NULL
I'm using WebAPI, restful api, C#, visual std 2015 我正在使用WebAPI,restful api,C#,visual std 2015
Thanks in Advance. 提前致谢。
EDIT; 编辑;
I'm using DHC chrome extention to POST data. 我正在使用DHC chrome扩展来发布数据。 HEADER; 标题; Content-Type: application/json BODY; 内容类型:application / json BODY;
{
"data" : {
"username" : "demo",
"password" : "demo1082*098/*42a",
"LoginToken" : "AAFF540EC55DASEFBE7E3D8404AED31F6DD30CA2BFCE2433B9475E696GG38730"
}
}
Error; 错误;
500 Internal Server Error 500内部服务器错误
EDIT; 编辑; I fount the error which is beacause of "string" kind of data. 我发现错误是因为“字符串”类型的数据。 I solved the problem when I change it as "dynamic". 当我将其更改为“动态”时,我解决了该问题。 Can someone recommend me a keyword instead of dynamic ? 有人可以推荐我一个关键字而不是动态的吗?
1) You have to create a class like 1)您必须创建一个类
public class LoginData
{
public string username {get;set;}
public string password {get;set;}
public string LoginToken {get;set;}
}
and in your web service use like following 并在您的网络服务中使用如下
public string Post([FromBody]LoginData data)
2) I seen following line that you already created a class name Login
(if you have single record in json) 2)我看到以下行您已经创建了一个类名Login
(如果您在json中有一条记录)
Login datas = JsonConvert.DeserializeObject<Login>(data);
So you also can use 所以你也可以使用
public string Post([FromBody]Login data)
One of the way to do it as follows, 执行此操作的一种方法如下:
Create class of your request 创建您的要求的课程
public class Data
{
public string username { get; set; }
public string password { get; set; }
public string LoginToken { get; set; }
}
public class RootObject
{
public Data data { get; set; }
}
and in your post method 和你的帖子方法
public string Post([FromBody]RootObject data)
Now you even don't have to deserialise it. 现在,您甚至不必反序列化它。 and can access direct as property of class. 并可以作为类的属性直接访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.