1

How can I get the values in Backend? Key is null and login is false. [FromQuery], [FromBody] or [FromForm] did not work

net core Application (Frontend)

HttpResponseMessage httpResponse = await client.PostAsJsonAsync("stamptimes", new { key, login });

net core Api (Backend)

[HttpPost]
public JsonResult PostNewStamptime(string key, bool login)
    {...}
4
  • 1
    you send an object and your backend excepts deconstructed values. your API should look like PostNewStamptime([FromBody] LoginModel model). And LoginModel should contain key and login properties. Commented Jun 26, 2019 at 11:29
  • A model for one method sounds like a workaround. is it my best option? Commented Jun 26, 2019 at 11:31
  • AFAIK, model for Post methods is the commonly-held approach. Maybe You can alter endpoints statusVerb to Get and send the parameters in query strings. That's when you can reach them via [FromQuery]. But this is semantically wrong. Commented Jun 26, 2019 at 11:35
  • 1
    Thank you! Post your comment as Answer and i mark it as solution Commented Jun 26, 2019 at 11:59

1 Answer 1

2

you send an object and your backend excepts deconstructed values. your API should look like below;

[HttpPost]
public JsonResult PostNewStamptime([FromBody] LoginModel model)
    {...}

And LoginModel should contain key and login properties like Below

public class LoginModel {
    public string Key {get;set;}
    public bool Login {get;set;}
}

Also, you can avoid using a model by altering endpoints statusVerb to Get and send the parameters in query strings. That's when you can reach them via [FromQuery]. But this is semantically wrong.

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

Comments

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.