Receiving Null value every time to Controller. How can I solve this?
Url: http://localhost:45801/Account/Login?ReturnUrl=%2Forder%2Fplaceorder
Code:
[HttpPost]
public async Task<IActionResult>Login(UserLoginVM userLoginVM,string ReturnUrl)
{
if (ModelState.IsValid)
{
var result = await signInManager.PasswordSignInAsync
(userLoginVM.LoginId, userLoginVM.Password, userLoginVM.RememberMe, false);
if(result.Succeeded)
{
if(!string.IsNullOrEmpty(ReturnUrl))
{
return RedirectToAction(ReturnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
ModelState.AddModelError("","Invalid username and password");
}
return View(userLoginVM);
}
2
Answers
You need to add
[FromBody]
beforeUserLoginVM
and[FromQuery]
beforestring
, so your action method sign should be like this :You should add
[FromQuery]
or[FromBody]
attribute to the parameters.Adding
[FromBody]
attribute to the parameter, force Web API to read from the request body.Adding
[FromQuery]
attribute to the parameter, force Web API to gets values from the query string.