在ASP.NET MVC中映射自定义路由

Jop*_*per 2 routing asp.net-mvc-3

我在Global.ascx中有这样的路由映射:

routes.MapRoute(
               "Help", // Route name
               "Help", // URL with parameters
               new { controller = "Home", action = "Help", id = UrlParameter.Optional } // Parameter defaults
            );
Run Code Online (Sandbox Code Playgroud)

因此,当用户键入http://mysite.com/Help时,他将收到回复来自Home.Help操作.

但是,如果我尝试使用参数id=something http://mysite.com/Help/something调用该路由, 则会收到错误消息The resource cannot be found.

我怎么能解决这个问题?

Rus*_*Cam 5

您需要{id}路由的URL模式中的路由值标记.

routes.MapRoute(
    "Help", // Route name
    "Help/{id}", // URL with parameters
    new { controller = "Home", action = "Help", id = UrlParameter.Optional } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)