简体   繁体   English

如何从 axios.post 发送简单值到 asp.net 核心?

[英]How send simple value from axios.post to asp.net core?

Using axios.post for send a simple value int to my controller on asp.net core, when send any value ever the method controller receive value "0" .使用 axios.post 将一个简单的值 int 发送到我在 asp.net core 上的控制器,当发送任何值时,方法控制器接收值 "0"

Which is the correct way for send this type of value using axios (post or delete)?使用 axios(发布或删除)发送此类值的正确方法是什么?

PD: i can send correctly models and receive on controller with [FromBody] PD:我可以使用 [FromBody] 正确发送模型并在控制器上接收

Method controller:方法控制器:

[Route("Delete"),HttpPost]
public async Task<ActionResult> Delete(int id)
    {
        try{
             var result = await userService.DeleteUserPerson(id);
             return Json(new{
                response=true,
                data=result,
                error=""
                });
        }
        catch(Exception ex){
            return Json(new{
                response=false,
                data=false,
                error=ex.Message
                });
        }
    }

Method from react class:来自反应类的方法:

async function DeleteUser(id, props){
var request= new Request({});
try{
    console.log(id);
    var axiosResp= await request.axios_request.post('User/Delete', JSON.stringify({id:id}));
    if(axiosResp.status!=200){
        //do smething
    }

    //this case validate error
    if(axiosResp.data.response && !axiosResp.data.error){
        //do something
    }

    //do something
}catch(err){
   //do something
}
}

Class request (axios):类请求(axios):

export default class Request {
constructor(){
    this.axios_request = axios.create({
        baseURL: 'http://localhost:5000/api',
        timeout: 5000,
        headers: authHeader()
    });
}

}

Testing diferents ways, this work for me:测试不同的方式,这对我有用:

 [Route("Delete/{id}"),HttpDelete("{id}")]
public async Task<ActionResult> Delete([FromRoute]int id){}

On axios use delete:在 axios 上使用删除:

request.axios_request.post('User/Delete/'+ id);

It looks like you are trying to do two different things at once.看起来您正在尝试同时做两件不同的事情。 So you can either specify the id in route or in the body of the HTTP request.因此,您可以在路由或 HTTP 请求正文中指定 id。 If it is in the route the url which should be called is "User/Delete/{id}".如果它在路由中,则应该调用的 url 是“User/Delete/{id}”。 In that case you should specify [FromRoute] before the id parameter of the function "Delete".在这种情况下,您应该在函数“Delete”的 id 参数之前指定 [FromRoute]。 like this (I would recommend using the HTTP delete for this: you can read about using delete with axios here: Axios Delete request with body and headers? ):像这样(我建议为此使用 HTTP 删除:您可以在此处阅读有关使用带有 axios 的删除的信息: 带有正文和标头的 Axios 删除请求? ):

[Route("Delete")]
[HttpDelete]
public async Task<ActionResult> Delete([FromRoute] int id) 

If you want to specify the id in the body you should do, as you mention yourself, use the [FromBody] like this:如果你想在你应该做的正文中指定 id,正如你自己提到的,像这样使用 [FromBody]:

[Route("Delete")]
[HttpPost]
public async Task<ActionResult> Delete([FromBody] int id) 

If you want to delete a model then I would suggest actually using the HTTP method delete instead.如果你想删除一个模型,那么我建议实际上使用 HTTP 方法删除。 You should then use the decorator [HttpDelete] instead of [HttpPost] as shown above.然后,您应该使用装饰器 [HttpDelete] 而不是 [HttpPost],如上所示。

Edit: Furthermore I can see that you are sending an object containing the parameter id to the controller.编辑:此外,我可以看到您正在向控制器发送一个包含参数 id 的对象。 Try either just sending the number or change the parameter of the function to an object containing the id to match what you are sending in your axios call.尝试仅发送数字或将函数的参数更改为包含 id 的对象,以匹配您在 axios 调用中发送的内容。

This means changing this line:这意味着改变这一行:

var axiosResp= await request.axios_request.post('User/Delete', JSON.stringify({id:id}));

to:到:

var axiosResp= await request.axios_request.post('User/Delete', id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM