基本 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”)。我必须如何在这里配置我的路线?
我要打
- Index() 与 '/area/'
- 带有“/area/123”和
的详细信息(int id)
- Detail(MyViewModel myViewModel) 带有 post 到 '/area/'
编辑
我知道我可以将 [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
}
我的控制器上有 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”)。我必须如何在这里配置我的路线?
我要打
- Index() 与 '/area/'
- 带有“/area/123”和 的详细信息(int id)
- Detail(MyViewModel myViewModel) 带有 post 到 '/area/'
编辑
我知道我可以将 [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
}