Home > database >  ASP.NET ModelState.IsValid is validating returnUrl parameter
ASP.NET ModelState.IsValid is validating returnUrl parameter

Time:12-20

I've already spent hours finding someone with the same issue, by googling and it seems like nobody else has the same issue. So I am creating my first post here :)

I have a method inside a controller, which is supposed to log-in the user and afterwards redirect the user to the url which he was previously trying to reach. But ModelState.IsValid is not only validating the model, but also the url-Parameter and if the user is NOT trying to get redirected (user is at the 'root-page' and clicks the button to login - in other words the returnUrl is null) and then it would always tell me that the validation is unsuccesful. I do not want to remove this error all the time manually, but find a different solution, where it would not try to validate this parameter.

[AllowAnonymous]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login([Bind("Email,Password")] LoginViewModel data, string returnUrl)
{
if (!ModelState.IsValid) return View(data);
   //todo throw custom error if user does not exist
   ClassUser? user = _context.Users.FirstOrDefault(x => x.Email == data.Email);
   //...
}

Click here to see ModelState's output

Thank you very much in advance..I am trying for hours to solve this already..

CodePudding user response:

Just put a question mark at the end of your last parameter datatype like this string? returnUrl or try Nullable<string> returnUrl

  • Related