Rajesh Sivasankaran
Rajesh Sivasankaran

Reputation: 136

MVC asp.net core to webapi service call pass null values

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

Upvotes: 0

Views: 1296

Answers (2)

Sarvesh Kumar
Sarvesh Kumar

Reputation: 3

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();
        }
    }
}

Upvotes: 0

Ranjith Varatharajan
Ranjith Varatharajan

Reputation: 1694

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.

Upvotes: 1

Related Questions