ASP.NET MVC路由映射问题

我正在努力在asp.net mvc 2中生成出站URL。这是场景。 控制器:MoveController 行动:索引() 查看:Index.aspx 现在我想要的是将多个url映射到具有不同参数(locationId)值的同一控制器(MoveController)和action(Index) 例如 url - > RouteData / Production / - > Move / Index / 1 /安装/ - >移动/索引/ 2 / Move / 3 / - > Move / Index / 3 我的映射看起来像这样:
 public static void RegisterRoutesTo(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{*favicon}", new
        {
            favicon = @"(.*/)?favicon.ico(/.*)?"
        });

        routes.MapRoute(
           null,                                              // Route name
           "Production/{locationId}",                           // URL with parameters
           new
           {
               controller = "Move",
               action = "Index"
           },  // Parameter defaults
           new
           {
               locationId = @"d+"
           }// constraint for Production url
       );

        routes.MapRoute(
           null,                                              // Route name
           "Installation/{locationId}",                           // URL with parameters
           new
           {
               controller = "Move",
               action = "Index"
           },  // Parameter defaults
           new
           {
               locationId = @"d+"
           }// constraint for Production url
       );

        routes.MapRoute(
           null,                                              // Route name
           "Move/{locationId}",                           // URL with parameters
           new
           {
               controller = "Move",
               action = "Index"
           },  // Parameter defaults
           new
           {
               locationId = @"d+"
           }// constraint for Production url
       );

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new
            {
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            }  // Parameter defaults
        );
    }
现在要在我的母版页菜单中生成出站网址,我正在做一些类似的事情
<%= Html.ActionLink("Production", "Index", "Move", new{locationId = 1}, null)%>
<%= Html.ActionLink("Installation", "Index", "Move", new{locationId = 2}, null)%>
但以上产生 /生产/ 1 /生产/ 2 这是正确的,但我怎么能告诉它生成 / production /当locationId = 1时 / installation / when locationId = 2 任何的想法? 等待,     
已邀请:
远射,但绝对值得一试,因为你上面的路线没有表达你想要的东西:
routes.MapRoute(
   null,                                              // Route name
   "Production/",                           // URL with parameters
   new
   {
       controller = "Move",
       action = "Index",
       locationId = 1
   }
);
routes.MapRoute(
   null,                                              // Route name
   "Installation/",                           // URL with parameters
   new
   {
       controller = "Move",
       action = "Index",
       locationId = 2
   }
);     
您是对的,生产和安装路线完全相同。我只是不知道如何区分它们,因为它们都使用相同的参数{locationId}但具有不同的值。正如您所看到的,我提供的默认值不同,但这只是一个默认值,因此我尝试对它们设置约束
routes.MapRoute(
               null,                                              // Route name
               "Production/{locationId}",                           // URL with parameters
               new
               {
                   controller = "Move",
                   action = "Index",
                   locationId = 1
               }
               ,  // Parameter defaults
               new
               {
                   locationId = @"1"
               }// constraint for Production url
           );

            routes.MapRoute(
               null,                                              // Route name
               "Installation/{locationId}",                           // URL with parameters
               new
               {
                   controller = "Move",
                   action = "Index",
                   locationId = 2
               }
               ,  // Parameter defaults
               new
               {
                   locationId = @"2"
               }// constraint for Production url
           );
这有效,但后来我意识到,在定义Html.ActionLink时,为参数赋予null值
<%= Html.ActionLink("Installation", "Index", "Move", null, null)%>
它产生了生产/网址,它应该是订单中的第一个。 我不知道的是,有必要在网址中定义{parameters}。我一直认为需要在url中定义参数。 感谢所有帮助人员     
提供的ActionLink帮助程序扩展不会这样做。您需要编写一个新的自定义ActionLink方法,该方法将根据您的需要有条件地呈现。     

要回复问题请先登录注册