Mvc 6 taghelper asp-动作路线不工作

Mvc 6 taghelper asp-action route not working

我在 Mvc 6 中使用区域并尝试路由到每个特定区域。 例如我有这个:

我的控制器外观装饰有 Area 和 route 属性,如下所示:

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

然后,我定义了两个 HTTPGET 方法,如下所示:

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

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

最后我的表格是这样的:

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

当我尝试 post 表单时,它会抛出以下内容:

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

当我在 asp-action 中定义它时,您会认为它会绑定到我的 Create 方法,但是当我检查表单的渲染标记时,它看起来像这样:

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

我的方法 Create 没有被处理,它只呈现 Test/Test (area/controller).

我也尝试用 [Route("Create")][HttpGet("Create")] 装饰我的方法,但没有结果。

我是不是漏掉了一些明显的东西?

如果您正在使用 RC1,那么您不能在标签助手中使用区域。

标签助手中的区域在 RC2 link to issue

中受支持