gpt4 book ai didi

c# - ReturnUrl 在使用 ASP.NET Core 6 MVC 的 POST 上始终为 null

转载 作者:行者123 更新时间:2023-12-05 05:42:55 26 4
gpt4 key购买 nike

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

POST 方法中添加断点时,returnurl 始终为null。但是对于 .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; }
}
}

Controller

[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>

最佳答案

“我无法使用 ASP.NET Core 6 MVC 让 ReturnUrl 在 HttpPost 上工作。”:

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

问题复制:

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

enter image description here

导致问题的原因:

If you investigate your code again you would noticed that you areusing asp-controller="Login" asp-action="Login" twice on yourLogin.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.

在表单的开头:

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

在您的按钮上提交::

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

解决方案:

最简单的解决方案就是像下面这样修改您的提交按钮代码,这将解决您的问题:

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

输出:

enter image description here

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

关于c# - ReturnUrl 在使用 ASP.NET Core 6 MVC 的 POST 上始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71930150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com