Monday, May 20, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
36
rated 0 times [  38] [ 2]  / answers: 1 / hits: 17576  / 5 Years ago, sun, april 28, 2019, 12:00:00

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.



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()
});
}

}

More From » c#

 Answers
18

Testing 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);

[#52180] Monday, April 22, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sidneyh

Total Points: 118
Total Questions: 108
Total Answers: 105

Location: Mali
Member since Fri, Jun 18, 2021
3 Years ago
sidneyh questions
Tue, Jun 7, 22, 00:00, 2 Years ago
Wed, Apr 13, 22, 00:00, 2 Years ago
Wed, Aug 12, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Fri, Apr 24, 20, 00:00, 4 Years ago
;