admin管理员组文章数量:1277901
Using axios.post for send a simple value int to my controller on asp core, when send any value ever the method controller receive value "0".
Which is the correct way for send this type of value using axios (post or delete)?
PD: i can send correctly models and receive on controller with [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):
export default class Request {
constructor(){
this.axios_request = axios.create({
baseURL: 'http://localhost:5000/api',
timeout: 5000,
headers: authHeader()
});
}
}
Using axios.post for send a simple value int to my controller on asp core, when send any value ever the method controller receive value "0".
Which is the correct way for send this type of value using axios (post or delete)?
PD: i can send correctly models and receive on controller with [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):
export default class Request {
constructor(){
this.axios_request = axios.create({
baseURL: 'http://localhost:5000/api',
timeout: 5000,
headers: authHeader()
});
}
}
Share
Improve this question
asked Apr 28, 2019 at 4:17
Dr oscarDr oscar
4151 gold badge6 silver badges21 bronze badges
2 Answers
Reset to default 4Testing diferents ways, this work for me:
[Route("Delete/{id}"),HttpDelete("{id}")]
public async Task<ActionResult> Delete([FromRoute]int id){}
On axios use delete:
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. If it is in the route the url which should be called is "User/Delete/{id}". In that case you should specify [FromRoute] before the id parameter of the function "Delete". like this (I would remend using the HTTP delete for this: you can read about using delete with axios here: Axios Delete request with body and headers?):
[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:
[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. You should then use the decorator [HttpDelete] instead of [HttpPost] as shown above.
Edit: Furthermore I can see that you are sending an object containing the parameter id to the controller. 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.
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);
本文标签: javascriptHow send simple value from axiospost to aspnet coreStack Overflow
版权声明:本文标题:javascript - How send simple value from axios.post to asp.net core? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741225235a2361732.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论