Search code examples
c#asp.net-mvcactionmethod-signatureambiguous-call

Ambiguous action methods with different HttpMethod


I'm experiencing a weird behaviour, at least to me. I written two methods within a controller with apparently different signatures:

[Route("~/Wallets/{walletId}/Transactions/Add")]
public async Task<ActionResult> Add(long walletId)

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Add(AddTransactionViewModel model)

The thing is every time I try to call the POST method using Ajax.BeginForm the GET method (the first) gets called.

@using (Ajax.BeginForm("Add", "Transactions", 
    new AjaxOptions() { HttpMethod = "POST" }) 
{ 
    ... 
}

Now, why this is happening? Of course if I change the name of the GET method say to AddTransaction the code works, but I want to understand why it doesn't as it is.


Solution

  • This is because BeginForm uses GetVirtualPath internally to get the url from the route table. The first link is added to the route table in your example.

    Simply editing the POST method with the following should do the trick:

    [HttpPost]
    [ValidateAntiForgeryToken]
    [Route("Add")]
    public async Task<ActionResult> Add(AddTransactionViewModel model)