Api 丢失了丢失的参数 [英] Api lost lost param
本文介绍了Api 丢失了丢失的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码来运行我的 api 方法
Hi I have this code to run my api method
export class MessageService {
constructor(private http: Http) {
}
addMessage(textToSend: string) {
return this.http.post("/api/SendMessage", textToSend); //<- Everytime i have some text in textToSend and this is ok
}
}
在我的 Api 之后,我的参数等于 null
And after in my Api my param is equals to null
[HttpPost]
[Route("/api/SendMessage")]
public void SendMessage(string msg) //null value
{
//some code
}
推荐答案
可能的解决方案 1:
addMessage(textToSend: string) {
let body = JSON.stringify({ textToSend });
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post("/api/SendMessage/", body, options);
}
// Server side -1
[HttpPost]
[Route("/api/SendMessage")]
public void SendMessage([FromBody]IDictionary<string, string> msg)
{
var textToSend = msg["textToSend"];
}
// Or create a model and use it
//Server side -2
public class Model
{
public string textToSend { get; set; }
}
public void SendMessage([FromBody]Model model)
可能的解决方案 2:
addMessage(textToSend: string) {
return this.http.post("/api/SendMessage/" + textToSend);
}
[HttpPost]
[Route("/api/SendMessage/textToSend")]
public void SendMessage(string textToSend)
{
//some code
}
这篇关于Api 丢失了丢失的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文