开发者

ASP.NET MVC Routes Mapping issue

开发者 https://www.devze.com 2023-01-15 06:12 出处:网络
I am struggling with generating outbound urls in asp.net mvc 2. Here is the scenario. Controller: MoveController

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
           {
         开发者_JAVA百科      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,


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
   }

);


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消