2

我有一个 ASP.NET MVC4 网站(使用 vs2012),允许用户通过单击“登录”链接登录。我收到了臭名昭著的错误:

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

我在这里检查了一堆链接,包括:

解决歧义 不明确的操作方法调用,出于某种原因 ASP.NET MVC 3

还有一些在SO之外。我已经确保我正确装饰了方法,但仍然找不到发生这种情况的原因。

这是我的“登录”链接代码:

    <ul class="topnav navRight">
        @if (!User.Identity.IsAuthenticated)
        {
            <li><a href="@Url.Action("Login", "Account")" id="ViewLogin">LOGIN</a></li>
        }
        else
        {
            <li><a href="@Url.Action("Index", "ClientStats")">STATS</a></li>
            <li><a href="@Url.Action("LogOut", "Account")">LOGOUT</a></li>
        }
    </ul>

和我的控制器:

[Authorize]
public class AccountController : CustomController
{
    //
    // GET: /Account/Logout
    public ActionResult LogOut()
    {
        WebSecurity.Logout();
        return RedirectToAction("Index", "Home");
    }

[AllowAnonymous]
public virtual ActionResult Login()
{
    return View();
}

[HttpPost]
[AllowAnonymous]
public virtual ActionResult Login(Clients model)
{
    if (ModelState.IsValid)
    {
        Security security = new Security();

        if (WebSecurity.Login(model.Username, model.Password))
        {
            using (MyProjectContext db = new MyProjectContext())
            {
                int userID = WebSecurity.GetUserId(model.Username);
                Data.User user = db.Users.Find(userID);

                if (user != null)
                    if (user.Active)
                    {
                        if (User.IsInRole("Administrator"))
                            return RedirectToAction("Admin", "ClientStats");
                        else
                            return RedirectToAction("Index", "ClientStats");
                    }
            }
        }
    }

    ModelState.AddModelError("", "Invalid Username or Password.");

    return View(model);
}

}

没有什么特别的CustomerController,但无论如何:

    public class CustomController : Controller
{
    public enum PageNames
    {
        Home,
        Services,
        Testimonials,
        Video,
        Photo,
        FAQ,
        About,
        Contact,
        Events,
        Profile
    }

    public int UserId
    {
        get { return Convert.ToInt32(Session["UserId"]); }
        set { Session["UserId"] = value; }
    }

    public static string GetPageTitle(PageNames pageName)
    {
        string pageTitle = "Welcome to My Website!";

        switch (pageName)
        {
            case PageNames.Services:
                pageTitle = "- Services";
                break;

            case PageNames.Testimonials:
                pageTitle = "- Testimonials";
                break;

            case PageNames.Video:
                pageTitle = "- Videos";
                break;

            case PageNames.Photo:
                pageTitle = "- Photos";
                break;

            case PageNames.FAQ:
                pageTitle = "- Frequently Asked Questions";
                break;

            case PageNames.Contact:
                pageTitle = "- Contact Us";
                break;

            case PageNames.Events:
                pageTitle = "- Calnedar of Events";
                break;

            case PageNames.Profile:
                pageTitle = "- Profile";
                break;
        }

        return pageTitle;
    }

    public static Data.User ClientInfo { get; set; }
}

这是路由信息...我尝试取消注释一条路由(并更改了它的名称),但我仍然收到相同的错误:

    public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}",
            defaults: new { controller = "Home", action = "Index" }
        );

        //routes.MapRoute(
        //    name: "Default",
        //    url: "{controller}/{action}/{id}",
        //    defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
        //);

        routes.MapRoute(
            name: "id",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

非常感谢任何帮助/指导!

更新 我已经尝试了注释掉路由值并添加[HttpGet]到登录方法的建议,但它仍然抛出相同的错误。我确实记得这个工作曾经有一次,所以我不知道最近发生了什么变化使它突然这样做。它可能是 Web.Config 中的东西吗?

4

2 回答 2

1

MVC doesn't support method overloading based solely on signature take [AllowAnonymous] out

add a [HttpGet] to the get actionresult and the problem should be solved

[HttpGet]
public virtual ActionResult Login()
{
    return View();
}
于 2013-12-05T23:35:42.287 回答
0

我遇到了这个问题,原来是我打开了 System.Web.Http 命名空间,它还定义了 [HttpGet]/[HttpPost] 属性。因此,这些属性优先于 System.Web.Mvc 中的属性使用,后者负责允许控制器中的方法重载,基于 Http 动词。注释掉

 open System.Web.Http 

解决了这个问题,允许 Mvc 选择正确的 ([HttGet]) 重载。

于 2014-04-06T17:46:45.150 回答