c# - How to convert Object and view it on WebAPI?
问题描述
Here is my question, I got a Web API and client(winform), client will send out data with a Serialize Object. My Web API do have received and return a response to client. But I can't view the data on Web API, I do have try using Deserialize Object and convert it into string but not working neither. Please help me,Thanks!
Here is my code:
Client
private string WebApiPost(string sParam, string sJson)
{
var client = new HttpClient();
var content = new StringContent(sJson, Encoding.UTF8, "application/json");
var response = client.PostAsync(sWebAPI_URL + sParam, content).Result;
var body = response.Content.ReadAsStringAsync().Result;
return body;
}
This is my Web API
public object Post([FromBody]object hL7)
{
//what should I do???
//I've tried set hL7 into string but it wont get any data;
//I've also tried deserialize it but will get 500 internal server error.
return hL7;
}
This is my WebAPI model
public class HL7MID
{
public string LOC { get; set; }
public string COMPANY { get; set; }
}
public class HL7MID_List
{
public string sMSG { get; set; }
public List<HL7MID> data = new List<HL7MID>();
}
解决方案
因为sJson
matches HL7MID
,您可以使用该类型作为Post
函数的参数,并且只使用该类型。
public HL7MID Post([FromBody]HL7MID hL7)
{
//use hL7 here
return hL7;//also since you know the return type, changing that to HL7MID is suggested
}
推荐阅读
- php - 无法从 PHP 中的 MySQL 获取非英文字母(文本)
- go - 我试图理解为什么这种情况总是会发生
- javascript - 为什么在公用文件夹中找不到我的 javascript 文件?
- r - 闪亮更新 plotOutput brushOpts
- css - h1定位问题
- c++ - 对整个数组进行冒泡排序
- php - if..else 嵌套在 oci 和 php 的 while 循环中,但 if...else 没有被调用
- openlayers - 有没有办法在 openlayers 5 中强制重新加载/刷新地图?
- python - Entrez (biopython):如何将术语搜索限制在特定期刊?(考研)
- android - 由于权限政策,我无法发布我的应用