0

I am trying to send data from React app to my ASP.NET Core Web API with the help of Axios, but I'm not getting the data.

Axios code:

    axios.put(urlPath,
                {
                        method: 'put',
                        data: {
 PageDbId: "Soursbh",
            UserDbId:""
}
                    })
                .then(response => onSuccess(response))
                .catch(e => onError(e));

ASP.NET Core model class:

public class WebAvatarModel
{
    public string PageDbId { get; set; }
    public string UserDbId { get; set; }
}

ASP.NET Core code:

[HttpPut]
public IActionResult Save_Avatar([FromBody] WebAvatarModel model)
{
    var users = model;
    
    return null;
}

Always getting null in PageDbId and UserDbId.

I also tried this:

await axios.put(urlPath, JSON.stringify(body),
                {
                    method: 'put',
                    headers: {'Content-Type': 'application/json'}
                })
            .then(response => onSuccess(response))
            .catch(e => onError(e));

[HttpPut]
public async Task<IActionResult> Save_Avatar(WebAvatarModel model)
{
    var users = model;
    return null;
}

But it's still not working. What am I doing wrong?

2
  • Have you configured the routing right on the .net core ( I don't see the attribute for example HttpPut) Commented Jun 23, 2020 at 6:00
  • I only removed the put attribute for testing. it is there but still not working Commented Jun 23, 2020 at 6:16

2 Answers 2

2

public IActionResult Save_Avatar([FromBody] WebAvatarModel model)

Did this api have [HttpPut]?

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I am using put request for updating the data
1

Always getting null in PageDbId and UserDbId

If you check the actual request payload using browser developer tool, you would find that the data look like below.

enter image description here

You need to modify your code as below to provide correct request payload.

axios.put(urlPath,
    {
        PageDbId: "Soursbh",
        UserDbId: ""
    })
    .then(response => onSuccess(response))
    .catch(e => onError(e));

Or

axios({
    method: 'put',
    url: urlPath,
    data: {
        PageDbId: "Soursbh",
        UserDbId: ""
    }
})
    .then(response => onSuccess(response))
    .catch(e => onError(e));

Test Result

enter image description here

1 Comment

Thank you so much for your help. finally, it's working as I was expected :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.