簡體   English   中英

MVC異常:以下操作方法之間存在歧義

[英]MVC Exception: Ambiguous between the following action methods

我收到此錯誤。

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

因此,您單擊了“登錄”按鈕,該按鈕被路由到“登錄”操作。 引起該錯誤的原因是,對於SignIn和Login操作,IsValidName的HttpParamActionAttribute返回true。

您的HttpParamActionAttribute針對Login操作針對IsValidName返回true,因為Login操作按名稱匹配。

現在,您在SignIn上的其他HttpParamActionAttribute也返回true,因為request [“ SignIn”]不等於null。

更改視圖以查找非“ LogIn”和“ SignIn”操作。 這樣,只有與按鈕名稱匹配的操作才會為IsValidName返回true。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM