1

我已经注册了这条路线:

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
4

1 回答 1

2

您期望第二段绑定到操作参数,但在您的路由中它是 id 参数。

"manage/{id}/{action}"

对于/manage/publishURL,id 参数的值为publish.

框架找不到动作参数,因此它使用默认值并将其重定向到索引动作。您只能将最后的参数作为可选参数。

如果必须在中间指定整数 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" }
            );

其他 URL 应回退到默认路由并正常工作。

于 2013-05-22T16:53:49.217 回答