使用 ASP.NET Core 6 MVC,ReturnUrl 在 POST 上始终为 null

ReturnUrl is always null on POST using ASP.NET Core 6 MVC

我无法使用 ASP.NET Core 6 MVC ReturnUrlHttpPost 上工作。

POST方法中添加断点时,returnurl始终为空。但是对于 .NET 5,除了 .NET 6 之外,它使用相同的代码设置,我需要使 returnurl 参数可以为空,这样我就不会收到错误“returnurl field is required”。

这是我正在使用的代码 - 任何帮助将不胜感激。

谢谢。

型号:

namespace IdentityManagerDotNet6.Models
{
    public class LoginViewModel
    {
        [Required]
        [EmailAddress]
        public string Email { get; set; } = string.Empty;

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

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }
}

控制器

[HttpGet]
public IActionResult Login(string? returnurl)
{
    ViewData["ReturnUrl"] = returnurl;
    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel loginViewModel, string? returnurl)
{
    ViewData["ReturnUrl"] = returnurl;
    returnurl = returnurl ?? Url.Content("~/");

    if (ModelState.IsValid)
    {
        var result = await _signInManager.PasswordSignInAsync(loginViewModel.Email, loginViewModel.Password, loginViewModel.RememberMe, lockoutOnFailure: true);

        if (result.Succeeded)
        {
            return LocalRedirect(returnurl);
        }

        if (result.IsLockedOut)
        {
            return View("Lockout");
        }
        else
        {
            ModelState.AddModelError(string.Empty, "Invalid login attempt.");
            return View(loginViewModel);
        }
    }

    return View(loginViewModel);
}

查看:

@model LoginViewModel

<h1 class="text-info">Log in</h1>
<div class="row">
    <div class="col-md-8">
        <form asp-controller="Account" asp-action="Login" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" role="form">
            <h4>Use a local account to log in</h4>
            <hr />
            <div asp-validation-summary="All" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Email" class="col-md-2"></label>
                <div class="col-md-10">
                    <input asp-for="Email" class="form-control" />
                    <span asp-validation-for="Email" class="text-danger"></span>
                </div>
            </div>
            <div class="form-group mt-3">
                <label asp-for="Password" class="col-md-2"></label>
                <div class="col-md-10">
                    <input asp-for="Password" class="form-control" />
                    <span asp-validation-for="Password" class="text-danger"></span>
                </div>
            </div>
            <div class="form-check mt-3">
                <input class="form-check-input" asp-for="RememberMe" type="checkbox" value="" id="flexCheckChecked">
                <label class="form-check-label" asp-for="RememberMe" for="flexCheckChecked">
                    Remember me?
                </label>
            </div>
            <div class="form-group">
                <div class=" col-1 my-3">
                    <button type="submit" asp-controller="Account" asp-action="Login" class="btn btn-success form-control">Login</button>
                </div>
            </div>
            <p>
                <a asp-action="Register">Register as a new user?</a>
            </p>
            <p>
                <a asp-action="ForgotPassword">Forgot your passord?</a>
            </p>
        </form>
    </div>
</div>

"I'm not able to get ReturnUrl to work on HttpPost using ASP.NET Core 6 MVC.":

I have checked your code between the line. It doesn't has anything wrong with [FromQuery] So you don't need to do anything on [FromQuery] as other answer I've seen, may be deleted now.

Issue Replication:

我已成功重现您的问题,如下所示:

What Causing the Issue:

If you investigate your code again you would noticed that you are using asp-controller="Login" asp-action="Login" twice on your Login.cshtml at the begining of the form and at the point of submit button this causing the data loss while you are submitting the form.

At the starting on form:

   <form asp-controller="Login" asp-action="Login" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" role="form">

At your button submit::

 <button type="submit" asp-controller="Login" asp-action="Login" class="btn btn-success form-control">Login</button>

Solution:

最简单的解决方案就是修改您的 submit button code,如下所示,这将解决您的问题:

            <div class="form-group">
                <div class=" col-1 my-3">
                    <button type="submit" class="btn btn-success form-control">Login</button>
                </div>
            </div>

Output:

希望它能彻底解决您的 returnurl null 问题。