Api丢失的参数[英] Api lost lost param

本文是小编为大家收集整理的关于Api丢失的参数的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

嗨,我有这个代码来运行我的api方法

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之后,我的param等于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
}

其他推荐答案

您的控制器操作是接受查询参数,而不是路由器参数或模型.

如果要接受路由参数,则需要将其添加到路由中.

如果要在正文中传递模型或值,则必须使用[FromBody]属性标记参数.

[HttpPost]
[Route("/api/SendMessage")]
public void SendMessage([FromBody]string msg)
{
    MessageBox MsgBox = new MessageBox();
    MsgBox.AddMsgToMsgBox(msg);
}

如果您没有定义任何内容,则控制器期望参数传递为查询/api/SendMessage?msg=someMessage(您在休息服务中不应该做,因为它不是很"RESTful"

其他推荐答案

尝试

 addMessage(textToSend: string) {
    return this.http.post("/api/SendMessage", msg); //<- Everytime i have some text in textToSend and this is ok 
}

更改了变量的名称以匹配您在控制器中的期望

中的一个

本文地址:https://www.itbaoku.cn/post/1937751.html

问题描述

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 
    }
}

And after in my Api my param is equals to null

[HttpPost]
[Route("/api/SendMessage")]
public void SendMessage(string msg) //null value
{
    //some code
}

推荐答案

Possible solution 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)

Possible solution 2:

addMessage(textToSend: string) {
    return this.http.post("/api/SendMessage/" + textToSend);
}

[HttpPost]
[Route("/api/SendMessage/textToSend")]
public void SendMessage(string textToSend)
{
    //some code
}

其他推荐答案

Your controller action is accepting a query parameter, not a router parameter or model.

If you want to accept a route parameter, you need to add it to the route.

If you want to pass a model or value in the body, you must mark the parameter with [FromBody] attribute.

[HttpPost]
[Route("/api/SendMessage")]
public void SendMessage([FromBody]string msg)
{
    MessageBox MsgBox = new MessageBox();
    MsgBox.AddMsgToMsgBox(msg);
}

If you don't define anything, the controller expects the parameter to be passed as query /api/SendMessage?msg=someMessage (which you shouldn't do in a REST service, as it's not very "RESTful"

其他推荐答案

try

 addMessage(textToSend: string) {
    return this.http.post("/api/SendMessage", msg); //<- Everytime i have some text in textToSend and this is ok 
}

changed the name of the variable to match the one you are expecting in the controller