C# ASP.NET MVC当前对X的请求不明确

C# ASP.NET MVC当前对X的请求不明确,c#,asp.net-mvc,asp.net-mvc-5,autofac,C#,Asp.net Mvc,Asp.net Mvc 5,Autofac,尝试登录时,我收到以下异常: The current request for action 'Login' on controller type 'AccountController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult Login() on type Sandbox.Web.Controllers.AccountController System.Web.Mvc.Actio

尝试登录时,我收到以下异常:

The current request for action 'Login' on controller type 'AccountController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Login() on type Sandbox.Web.Controllers.AccountController
System.Web.Mvc.ActionResult Login(Sandbox.Web.Models.Account.LoginModel) on type Sandbox.Web.Controllers.AccountController
我知道这是什么意思,但我不明白为什么我在登录时才收到它。 我的登录代码与我的所有其他操作一样已生成

public class AccountController : Controller
{
    // ctor...

    [AllowAnonymous]
    public ActionResult Login()
    {
       // ...
    }

    [AllowAnonymous]
    [HttpPost]
    public ActionResult Login(LoginModel model)
    {
       // ...
    }
}
我的其他动作都是这样的

ActionResult Create()
[HttpPost]
ActionResult Create(OrderModel model)

ActionResult Edit(int id)
[HttpPost]
ActionResult Edit(OrderModel model)
他们工作没有问题

我正在Mvc 5.2.7应用程序中使用Autofac 4.9和Autofac.Integration.Mvc(4.0)。 我从默认MVC模板更改的唯一部分是依赖项解析器

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
如果我将
[HttpGet]
放在
Login()之前,一切正常


有没有人知道如何让它工作或者为什么它不工作?

当您请求登录页面时,这是一个get请求,目前只有一个可能的方法,因为另一个方法是用HttpPost修饰的。因此,请求登录页面是不含糊的

当您提交登录表单时,这将成为Post请求。因为有一个标记为HttpPost的方法和一个未标记为NoAction的公共方法,所以这两个方法可能负责响应该请求。这是不明确的,代码不知道调用哪个


将HttpGet添加到第一个方法可以修复它,因为当您发送Post请求时,第一个方法不再被视为候选方法。

好吧,但是为什么它适用于我的
Create
Edit
操作?这里我只指定了
[HttpPost]
,它可以工作。编辑一个可以工作,因为它有一个不可为空的参数,这使得它是唯一的url和方法组合,创建一个不会显示所有的代码。它也是公共的吗?是的,它们都是公共的。如果我删除了
[AllowAnonymous]
一切正常(只要我不为所有路由启用授权)。我将检查我的Autofac配置。也许这就是问题所在