Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IActionResult parameter not being passed

I am POSTing the following request to

http://somesite.ngrok.io/nexmo?to=61295543680&from=68889004478&conversation_uuid=CON-btt4eba4-dbc3-4019-a978-ef3b230e923a&uuid=2ddh8172jbc252e02fa0e99445811fdc

The action controller method in my ASP.NET Core application is:

[HttpPost]
public IActionResult Index(string uuid)
{
    string s = uuid;
    return View();
}

When setting a breakpoint at the return line, why does s = null? It should equal 2ddh8172jbc252e02fa0e99445811fdc.

UPDATE

Adding [FromQuery] doesn't seem to be working. I contacted the API team and they got back to me saying that this is the information they were POSTing to my url:

"HTTP-METHOD=POST"
"HTTP-RESP=callback response is ignored"
"CONVERSATION-ID=CON-81593cc2-cba0-49e7-8283-f08813e1a98e"
"HTTP-REQ={from=61400104478, to=61290567680, uuid=8a4079c012c1bfa23f6dff8485e27d00, conversation_uuid=CON-81593cc2-cba0-49e7-8283-f08813e1a98e, status=started, direction=inbound, timestamp=2018-08-31T15:05:02.729Z}"

Is there anything from this information that helps? Maybe I have to deserialise something? I wouldn't have thought so but I don't understand why I am not retrieving the uuid value, and I can see the request has actually made it to my controller action...

like image 985
Harry Stuart Avatar asked Mar 25 '26 05:03

Harry Stuart


1 Answers

Because you did not tell the action to expect the parameter from the query string

[FromHeader], [FromQuery], [FromRoute], [FromForm]: Use these to specify the exact binding source you want to apply.

Create a model to hold the values

public class Model {
    public string uuid { get; set; }

    //...other properties
}

and update the action to use [FromQuery] to bind the model based on key-values provided in the query string

[HttpPost]
public IActionResult Index([FromQuery]Model model) {
    if(ModelState.IsValid) {
        var uuid = model.uuid;
        string s = uuid;
        return View();
    }
    return BadRequest();
}

Ideally for POST requests you should be taking advantage of the request BODY for more complex types as query string is more limited than the body of the request.

Reference Model Binding in ASP.NET Core

like image 69
Nkosi Avatar answered Mar 27 '26 17:03

Nkosi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!