基本 MVC 路由(模糊路由问题)

Basic MVC routing (amibigous route issue)

我的控制器上有 3 个动作,none 其中应该有一个与其参数不同的路径。

[Route("")]
[HttpGet]
public ActionResult Index()
{
// List of things
    return View();
}

[Route("")]
[HttpGet]
public ActionResult Detail(int id)
{
// Specific 'thing'
    return View();
}

[Route("")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Detail(MyViewModel myViewModel)
{
// 'Thing' has just been posted
    return View();
}

我在尝试调用第一个时出现 'call is ambiguous' 错误,在第二个时出现 404(即“/area/1001”)。我必须如何在这里配置我的路线?

我要打

编辑

我知道我可以将 [Route("{id:int}")] 添加到第二个操作,但不确定第三个操作。

[RoutePrefix("Area")]
public AreaController : Controller {
    //GET Area
    [Route("")]
    [HttpGet]
    public ActionResult Index() {
        // List of things
        return View();
    }

    //GET Area/123
    [Route("{id:int}")]
    [HttpGet]
    public ActionResult Detail(int id) {
        // Specific 'thing'
        return View();
    }

    //POST Area
    [Route("")]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Detail(MyViewModel myViewModel) {
        // 'Thing' has just been posted
        return View();
    }
}

确保配置了属性路由

public static void RegisterRoutes(RouteCollection routes) {

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapMvcAttributeRoutes();

    //...other code
}