ASP.NET MVC 路由映射问题

发布于 2024-09-18 10:10:10 字数 2825 浏览 6 评论 0原文

我正在努力在 asp.net mvc 2 中生成出站 url。这是场景。

控制器:移动控制器 动作:索引() View: Index.aspx

现在我想要的是将多个 url 映射到具有不同参数 (locationId) 值的同一控制器 (MoveController) 和操作 (Index),

例如

  • 网址 -> RouteData
  • /生产/ ->移动/索引/1
  • /安装/ ->移动/索引/2
  • /移动/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
        );
    }

现在要在我的母版页菜单中生成出站网址,我正在做类似ethis的操作

<%= Html.ActionLink("Production", "Index", "Move", new{locationId = 1}, null)%>
<%= Html.ActionLink("Installation", "Index", "Move", new{locationId = 2}, null)%>

但是上面生成的

  • /Production/1
  • /Production/2

是正确的,但我怎么知道 生成

  • 当 locationId =1 时
  • /Production/ /Installation/ 当 locationId = 2 时

有任何想法吗?

等待,

I am struggling with generating outbound urls in asp.net mvc 2. Here is the scenario.

Controller: MoveController
Action: Index()
View: Index.aspx

Now what I would like is to have multiple urls mapping to same controller (MoveController) and action (Index) with different parameter (locationId) value

e.g.

  • url -> RouteData
  • /Production/ -> Move/Index/1
  • /Installation/ -> Move/Index/2
  • /Move/3/ -> Move/Index/3

My mapping look like this:

 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
        );
    }

Now to generate the outbound urls in my master page menu, I am doing something lik ethis

<%= Html.ActionLink("Production", "Index", "Move", new{locationId = 1}, null)%>
<%= Html.ActionLink("Installation", "Index", "Move", new{locationId = 2}, null)%>

But the above generates

  • /Production/1
  • /Production/2

which is correct, but how can i tell it to generate

  • /Production/ when the locationId =1
  • /Installation/ when locationId = 2

Any Idea?

Awaiting,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰沐子 2024-09-25 10:10:10

可能性不大,但绝对值得一试,因为上面的路线没有表达你想要的:)

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
   }

);

long shot, but definitely worth a shot as your routes above weren't expressing what you want:

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
   }

);

饮惑 2024-09-25 10:10:10

您是对的,生产和安装路线完全相同。我只是不知道如何区分它们,因为它们都采用相同的参数 {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
           );

参数值为 null

<%= Html.ActionLink("Installation", "Index", "Move", null, null)%>

这可行,但后来我意识到,在定义 Html.ActionLink 时,它生成的 Production/ url,它应该是订单中的第一个。

我不知道的是,不必在 url 中定义 {parameters}。我一直认为参数需要在url中定义。

感谢大家的帮助

You are correct the Production and Installation routes are exactly the same. I just didn't know how to differentiate them, as they both take the same parameter {locationId} but with different values. As you can see I have provided the default value to be different, but thats just a default value, so therefore I tried putting constraint on them

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
           );

This works but then I realisez that when defining the Html.ActionLink giving value of null for parameters

<%= Html.ActionLink("Installation", "Index", "Move", null, null)%>

it produces the Production/ url, which it should as its the first one in the order.

What I didn;t know is that one don;t necessarliy have to define the {parameters} in the url. I always thought that the parameters need to be defined in the url.

Thanks for all the help guys

困倦 2024-09-25 10:10:10

提供的 ActionLink 帮助程序扩展不会执行此操作。您需要编写一个新的自定义 ActionLink 方法,该方法将根据您的需要有条件地呈现。

The provided ActionLink helper extension won't do that. You'd need to write a new, custom ActionLink method that will render conditionally as you require.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文