Trouble with Controller receiving data from an Axi...
# help-with-other
c
I have a controller that is getting hit by a post from an Axios function. So far so good. Unfortunatelythe controller can't read the data. I've googled all over the place adding routes (not needed it's being hit anyway) and tried [FromRoute], [FromBody], etc. in the constructor. All to no avail. The id is always null. If anyone has any suggestions I'd be very happy to hear them. Controller code is below, what's being sent is in the image:-
Copy code
[HttpPost]
// [Route("Users/DeleteUserNumber")]
public bool DeleteUserNumber([FromBody] string? id) {

    bool returnState = false;
    
    /*if (id != null  && Convert.ToInt32(id) > 0) {
        returnState = _UsersDataFunctions.DeleteUserNumber(id);
    }*/

    return returnState;
}
https://cdn.discordapp.com/attachments/1277376681082093590/1277376681606123591/image.png?ex=66ccf13d&is=66cb9fbd&hm=473618619eecaab457f1a6470c3b99f96124791dbf6597606460fd127f088cc5&
d
It looks like you're sending your payload as json, but attempting to read it as a string. I'm not sure why your value is
null
, but I think you may be able to solve it in one of two ways: *Option 1*: create a request model:
Copy code
csharp
public class MyRequestModel
{
    public string Id { get; set; }
}

public bool DeleteUserNumber([FromBody] MyRequestModel request)
{
    // ... your code
}
*Option 2*: Don't send the payload as json, but directly. Instead of sending this:
Copy code
json
{id: "000000015"}
Send this:
Copy code
000000015
c
Thanks. I thought dotnet core could automatically ingest simple JSON. Axiom sends as JSON anyway, as I understand it. First time I've used it. Also the first time I've done a pure MVC web site. I used to do a load of this stuff about 15 years ago when it was all webforms and VB (shiver!). I worked my way around it by using a GET. BUT, I'll have to use a post with a more complex model in the next couple of days so I'll let you know how it goes 🙂
d
Yeah, I would've expected your string to contain the json literally as string, so I'm not sure why you're getting null instead, but I've succesfully used both approaches that I mentioned
I'm a bit the other way around. My team insisted on using json APIs for all forms everywhere and that has caused a fair bit of issues, so we're now slowly reducing the amount of javascript that we're using. I'm trying to push us back to classic forms
50 Views