Search code examples
asp.net-coreasp.net-mvc-routingasp.net-core-mvctag-helpers

Mvc 6 taghelper asp-action route not working


I'm using Areas in Mvc 6 and trying to route to each specific area. For example I have this:

My controller looks is decorated with a Area and route attribute, like so:

[Authorize]
[Area("Test")]
[Route("[area]/[controller]")]
public class TestController : Controller

Then, I have two HTTPGET methods defined like below:

    public IActionResult Index()
    {
        var model = new TestViewModel();
        return View(model);
    }

    public IActionResult Create()
    {
        var model = new TestViewModel();
        return View(model);
    }

Finally my form looks like this:

    <form asp-route-area="Test" asp-controller="Test" asp-action="Create" asp-antiforgery="false" method="get">
        <input type="submit" value="Generate test"/>
    </form>

When I try to post the form it throws the following:

AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

Areas.Test.Controllers.TestController.Index

Areas.Test.Controllers.TestController.Create

You would think that it would bind to my Create method when I define it in the asp-action, but when I check the rendered markup of the form it looks like this:

<form method="get" action="/Test/Test">
        <input type="submit" value="Generate test">
 </form>

My method Create isn't handled, It only renders Test/Test (area/controller).

I've also tried to decorate my method with [Route("Create")] and [HttpGet("Create")] without results.

Am I missing out on something obvious here?


Solution

  • If you are using RC1 then you can't use areas in tag helpers.

    Areas in tag helpers are supported in RC2 link to issue