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

时间:2014-06-24 18:33:42

标签: c# asp.net-mvc asp.net-mvc-4

我收到此错误。

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

1 个答案:

答案 0 :(得分:2)

所以你点击了SignIn按钮,该按钮被路由到Login操作。导致该错误的原因是,对于SignIn和Login操作,HttpParamActionAttribute为IsValidName返回true。

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

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

更改视图以查找不是“LogIn”而非“SignIn”的操作。这样,只有匹配按钮名称的操作才会为IsValidName返回true。