0

in request data

enter image description here

json data

enter image description here

I don't know, why it pass null values to api?

Update 1

here is my webui action code

 const string URLPREFIX = "api/account";
    [HttpPost]
            public async Task<IActionResult> Login(LoginModel loginModel)
            {
                var loginFlag = false;
                HttpResponseMessage response1 = await ServiceCall<LoginModel>.postData(URLPREFIX + "/authenticate",loginModel);
                if (response1.IsSuccessStatusCode)
                {
                    loginFlag = await response1.Content.ReadAsAsync<bool>();
                }

                if (loginFlag)
                {
                    return RedirectToAction("Index","Home");
                }
                else
                {
                    return View();
                }

            }

update 2

public class LoginModel
    {
        [Required]
        [Display(Name = "Username")]
        public string Username { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }
    }

any try and reply, thanks

11
  • 1
    can you show your webapi request code ? Commented Dec 26, 2016 at 5:21
  • can you be more specific? Commented Dec 26, 2016 at 5:23
  • 1
    Where is the code for loginmodel Commented Dec 26, 2016 at 5:26
  • @hitesh the call is right there in the first image Commented Dec 26, 2016 at 5:27
  • ok, please show your LoginModel code Commented Dec 26, 2016 at 5:28

2 Answers 2

1

If you are using Json as a parameter to your controllers, use [formbody] in your web API. It should work. Let me know if it doesn't work.

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

1 Comment

i used [frombody] in MVC project it showed blank page when click, but using [frombody] in webApi project it working good, Thanks
0

You must add [ApiController] in controller level. code is following

[ApiController]
[Route("[controller]")]
public class LoginController : ControllerBase 
{
    const string URLPREFIX = "api/account";
    
    [HttpPost]
    public async Task<IActionResult> Login(LoginModel loginModel)
    {
        var loginFlag = false;
        HttpResponseMessage response1 = await ServiceCall<LoginModel>.postData(URLPREFIX + "/authenticate",loginModel);
        if (response1.IsSuccessStatusCode)
        {
            loginFlag = await response1.Content.ReadAsAsync<bool>();
        }

        if (loginFlag)
        {
            return RedirectToAction("Index","Home");
        }
        else
        {
            return View();
        }
    }
}

1 Comment

You must add [ApiController] in controller level. code is following [ApiController] [Route("[controller]")]

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.