Luca Celenza Luca Celenza - 16 days ago 6
C# Question

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.

Answer

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)