MVC异常:以下动作方法之间不明确

本文关键字:方法 之间 不明确 异常 MVC | 更新日期: 2023-09-27 18:10:03

我得到这个错误。

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");
}

MVC异常:以下动作方法之间不明确

所以您单击了路由到Login操作的SignIn按钮。该错误是由于登录和登录操作的IsValidName的HttpParamActionAttribute返回true导致的。

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

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

改变你的视图来寻找一个既不是"LogIn"也不是"SignIn"的动作。这样,只有与按钮名称匹配的操作才会为IsValidName返回true。