2

I receive the following exception when trying to login in:

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

Well I know that what it means, but I don't get why I receive it (only) at login. My login code has been built like all my other actions

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

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

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

My other actions are built like

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

ActionResult Edit(int id)
[HttpPost]
ActionResult Edit(OrderModel model)

and they work without problems.

I am using Autofac 4.9 with Autofac.Integration.Mvc (4.0) within a MVC 5.2.7 application. The only part I changed from default MVC template is the dependency resolver

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

If I place a [HttpGet] before Login() everything works well.

Anyone having an idea on how to get this working or why it does not work?

Raphael
  • 990
  • 1
  • 13
  • 24

1 Answers1

0

When you request the login page, that is a Get request, which currently only has 1 possible method, because the other method is decorated with HttpPost. Therefore requesting the login page is non-ambiguous.

When you submit the login form, that becomes a Post request. Since there is a method marked HttpPost and a public method that isn't marked as NonAction, then those two could be responsible for answering that request. It is ambiguous, the code doesn't know which one to call.

Adding HttpGet to the first method fixes it because the first method is no longer seen as a candidate when you send in a Post request.

Slicksim
  • 7,054
  • 28
  • 32
  • Well, but why does it work for my `Create` and `Edit` actions? There I only specified `[HttpPost]` and it works. – Raphael Feb 19 '19 at 11:26
  • The edit one works because it has a non-nullable parameter, which makes it unique url and method combo, the create one doesn't show all the code. Is it public too? – Slicksim Feb 19 '19 at 14:29
  • Yes they are all `public`. If I remove `[AllowAnonymous]` everything works well (as long as I don't enable authorization for all routes). I will check my Autofac configuration. Maybe that's the problem – Raphael Feb 20 '19 at 06:42