1

I have created simple .net core web api. But when I chek it by postman my model parameter values are null.

namespace Identity.API.Controllers {

[Produces("application/json")]
[Route("api/Token")]
//[AllowAnonymous]    
public class AccountController : Controller
{
    [HttpPost] 
    public IActionResult Token(LoginViewModel model)
    {
         //some code here

        return Ok(token.Value);
    }              
}

}

enter image description here

but parameter values are null

enter image description here

please help me.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
isanka thalagala
  • 456
  • 2
  • 10
  • 22

1 Answers1

3

You are sending the parameters as a JSON string, but your API is not expecting that. It is expecting the parameters either in the query string (GET) or as form variables (POST). Try sending them as form variables and it will work.

If you really want to send the parameters as a JSON string, you need to set the Content-Type header to application/json. Additionally, you need to mark them with [FromBody] like this:

public IActionResult Token([FromBody] LoginViewModel model){
}
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
  • https://stackoverflow.com/a/24629106/496680 – Steve's a D Dec 23 '17 at 19:02
  • @Steve'saD That link is not for Core, so although most of it is still valid for Core, there are some differences. – Racil Hilan Dec 23 '17 at 20:09
  • thanks I didn't realize that was different in Core. I didn't downvote you. – Steve's a D Dec 24 '17 at 00:00
  • @Steve'saD It doesn't matter for the downvote even if you did :). It's ironic to have a downvoted accepted answer :), but I guess because the question is marked as a duplicate. I answered it because the other answer didn't have an explanation. Yes, I don't like having so many little differences between Core and standard, especially when we have to maintain projects on different platforms. Who's going to remember all of that? – Racil Hilan Dec 24 '17 at 23:37