简体   繁体   English

MVC异常:以下操作方法之间存在歧义

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

I am getting this error. 我收到此错误。

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

Here is my Code 这是我的代码

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

So you clicked the SignIn button which is routed to the Login action. 因此,您单击了“登录”按钮,该按钮被路由到“登录”操作。 The error is caused because HttpParamActionAttribute is returning true for IsValidName for both SignIn and Login actions. 引起该错误的原因是,对于SignIn和Login操作,IsValidName的HttpParamActionAttribute返回true。

Your HttpParamActionAttribute returns true for IsValidName against the Login action because the Login action matches by name. 您的HttpParamActionAttribute针对Login操作针对IsValidName返回true,因为Login操作按名称匹配。

Now your other HttpParamActionAttribute on SignIn also returns true because request["SignIn"] is not equal to null. 现在,您在SignIn上的其他HttpParamActionAttribute也返回true,因为request [“ SignIn”]不等于null。

Change your view to look for an action that is not "LogIn" and also not "SignIn". 更改视图以查找非“ LogIn”和“ SignIn”操作。 That way only the action that matches the button name will return true for IsValidName. 这样,只有与按钮名称匹配的操作才会为IsValidName返回true。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MVC:以下方法或属性之间的调用不明确 - MVC: The call is ambiguous between the following methods or properties 在以下操作方法之间,当前对控制器类型“ ...”的操作请求[…]不明确 - The current request for action […] on controller type '…' is ambiguous between the following action methods 路由:当前的动作请求 […] 在以下动作方法之间是不明确的 - Routing: The current request for action […] is ambiguous between the following action methods 在以下操作方法之间,当前对控制器类型“ AdminController”的操作“ AdminManagement”的请求不明确 - The current request for action 'AdminManagement' on controller type 'AdminController' is ambiguous between the following action methods 控制器类型&#39;WeeklyTargetController&#39;上的当前动作&#39;Create&#39;请求在以下操作方法之间是不明确的: - The current request for action 'Create' on controller type 'WeeklyTargetController' is ambiguous between the following action methods: 错误:“以下方法或属性之间的调用不明确”? - Error: “The call is ambiguous between the following methods or properties”? 以下方法或属性之间的调用不明确: - The call is ambiguous between the following methods or properties: 以下方法或属性之间的调用是不明确的(bug ??) - The call is ambiguous between the following methods or properties (bug??) 以下方法或属性之间的调用不明确 - The call is ambiguous between the following methods or properties Xamarin - 以下方法或属性之间的调用不明确 - Xamarin - The call is ambiguous between the following methods or properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM