我的控制器上有3个操作,其中没有一个操作应该有一个除了参数之外的路径。
[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();
}
我在尝试呼叫第一个时遇到“呼叫是模糊的”错误,第二个呼叫是404(即“/ area / 1001”)。我该如何在这里配置我的路线?
我想点击
我知道我可以在第二个动作中添加[Route("{id:int}")]
,但不确定第三个动作。
答案 0 :(得分:2)
[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
}