MVC登录模糊动作 [英] MVC Ambiguous Action for Login

查看:100
本文介绍了MVC登录模糊动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got an ASP.NET MVC4 website (using vs2012) that allows users to login by clicking the "login" link. I'm getting the infamous error:

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

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

我在这里检查了很多链接,包括:

I've checked a bunch of links on here, including:

解决歧义 由于某些原因而导致的歧义操作方法调用. NET MVC 3

以及SO之外的其他一些内容.我确定我的方法装饰正确,但是仍然找不到发生这种情况的原因.

and a few more outside of SO. I've made sure I have the methods decorated correctly but still can't find the reason this is happening.

这是我的登录"链接的代码:

Here's my code for the "Login" link:

    <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并没有什么特别之处,但无论如何都如此:

The CustomerController has nothing special in it but here it is anyways:

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

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

And here's the routing information...I've tried to uncomment the one route (and changed it's name) but I still get the same error:

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

非常感谢您的帮助/指导!

Any help/guidance is greatly appreciated!

更新我已经尝试了注释路由值并将[HttpGet]添加到Login方法的建议,但仍然会引发相同的错误.我确实记得一次可以完成这项工作,所以我不知道最近发生了什么变化以使其突然执行此操作.可能是Web.Config中的内容吗?

UPDATE I've tried the suggestions of commenting out the routing values and adding [HttpGet] to the Login method but it still throws the same error. I do recall this working at one time so I don't know what has changed lately to make it do this all of a sudden. Could it be something in the Web.Config?

推荐答案

MVC不支持仅基于签名的方法重载 删除[AllowAnonymous]

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

在get actionresult中添加[HttpGet]并应解决问题

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

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

这篇关于MVC登录模糊动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆