gpt4 book ai didi

c# - MVC 异常 : Ambiguous between the following action methods

转载 作者:行者123 更新时间:2023-11-30 22:05:24 25 4
gpt4 key购买 nike

我遇到了这个错误。

The current request for action 'Login' on controller type 'AccountController' is ambiguous between the following action methods:  
System.Web.Mvc.ActionResult Login(MVCApp.Models.Account) on type MVCApp.Controllers.AccountController
System.Web.Mvc.ActionResult SignIn(MVCApp.Models.Account) on type MVCApp.Controllers.AccountController

这是我的代码

<input type="submit" name="Login" value="Login" />
<input type="submit" name="SignIn" value="SignIn" />
public class HttpParamActionAttribute : ActionNameSelectorAttribute
{
public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
{
if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase))
return true;

var request = controllerContext.RequestContext.HttpContext.Request;
return request[methodInfo.Name] != null;
}
}

public ActionResult Login()
{
return View();
}

[HttpPost]
[HttpParamAction]
public ActionResult Login(Account account)
{
Account createAccount = new Account();

createAccount.Username = account.Username;
createAccount.Email = account.Email;
createAccount.Password = account.Password;

return View("Login");
}

// GET: /Account/SignUp

public ActionResult SignIn()
{
return View();
}

[HttpPost]
[HttpParamAction]
public ActionResult SignIn(Account account)
{
Account createAccount = new Account();

createAccount.Username = account.Username;
createAccount.Email = account.Email;
createAccount.Password = account.Password;

return View("SignUp");
}

最佳答案

因此,您单击了路由到登录操作的登录按钮。导致错误的原因是 HttpParamActionAttribute 为登录和登录操作的 IsValidName 返回 true。

您的 HttpParamActionAttribute 针对登录操作针对 IsValidName 返回 true,因为登录操作按名称匹配。

现在您在 SignIn 上的其他 HttpParamActionAttribute 也返回 true,因为 request["SignIn"] 不等于 null。

更改您的 View 以查找既不是“登录”也不是“登录”的操作。这样只有与按钮名称匹配的操作才会为 IsValidName 返回 true。

关于c# - MVC 异常 : Ambiguous between the following action methods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24393856/

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