我注册了这条路线:
context.MapRoute(
"Manager",
"manage/{id}/{action}",
new { action = "index", controller = "manage", id = UrlParameter.Optional },
new string[] { "Web.Areas.Books.Controllers" }
);
然后我有这两个网址:
http://<site>/manage <-- hits the index action of managecontroller
http://<site>/manage/publish <-- ALSO HITS INDEX VIEW even I have publish action
可能缺少什么?
基本上,我需要一条路线来服务所有这些:
http://<site>/manage <-- should go to index action
http://<site>/manage/publish <-- should go to publish action
http://<site>/manage/delete <-- should go to delete action
http://<site>/manage/123123/update <-- should go to update action
答案 0 :(得分:2)
您希望将第二个分段绑定到动作参数,但在您的路线中它是id参数。
"manage/{id}/{action}"
使用/manage/publish
网址,id参数的值为publish
。
Framework无法找到操作参数,因此它使用默认值并将其重定向到Index操作。您最后只能将参数作为可选参数。
如果必须在中间指定和整数id,可以通过定义约束来使其工作。
context.MapRoute(
"Manager",
"manage/{id}/{action}",
new { action = "index", controller = "manage" },
new { id = @"\d+" }, //second segment has to be an integer
new string[] { "Web.Areas.Books.Controllers" }
);
其他网址应该回归到默认路由并工作。