网址映射:不希望在网址中键入索引

时间:2012-02-03 01:47:21

标签: asp.net-mvc-3 asp.net-mvc-routing maproute

我在Global.asax.cs中有以下URL地图:

 routes.MapRoute("RejectRevision", "{controller}/{Index}/{revisionId}"
        , new { controller = "RejectRevision", action = "Index", revisionId = "" });

但我不想输入http://localhost:9999/RejectRevision/Index/1,我希望能够输入http://localhost:9999/RejectRevision/1以便在RejectRevision控制器上点击Index操作。我在这里缺少什么?

感谢。

1 个答案:

答案 0 :(得分:1)

将它放在Default路线之前:

routes.MapRoute(
    "RejectRevision",
    "{controller}/{revisionId}",
    new { 
        controller = "RejectRevision", 
        action = "Index", 
        revisionId = UrlParameter.Optional }
);

如果将其放在Default路线之前,则/RejectRevision/1的请求将映射到RejectRevisionController.Index()操作方法。

或者,如果这是您希望像这样映射的唯一Controller / Action方法,那么您可以使用文字而不是路径的参数:

routes.MapRoute( 
    "RejectRevision", 
    "RejectRevision/{revisionId}", 
    new {  
        controller = "RejectRevision",  
        action = "Index",  
        revisionId = UrlParameter.Optional } 
);