0

Here's my simple actionlink:

<span>@Html.ActionLink(trip.TripDescription, "Index", "Home", new { trip = trip.Trpp, year = trip.TripYear })</span>

Here is my route for that link:

routes.MapRoute(
                "Index",
                "{controller}/{action}/{trip}/{year}",
                new { controller = "Home", action = "Index", trip = "", year = "" }
            );

and here is what is being rendered:

http://localhost:31065/Home/Index?trip=Green&year=2013

I'd like for it to render this instead:

http://localhost:31065/Home/Index/Green/2013

both of those urls work, but cosmetically I'd prefer the latter.

what do I need to change to have the parameters go behind slashes instead of using the old school ? and &

TIA

Christopher Johnson
  • 2,629
  • 7
  • 39
  • 70
  • I would expect it to work the way you have it. What if in the route definition you do not set `trip = "", year = ""`? – Andy T Aug 29 '13 at 18:44
  • Do you have any other routes defined before the one you showed? – dom Aug 29 '13 at 18:45
  • @QuetiM.Porta when I remove those parameters from the route, the url that gets rendered is: http://localhost:31065/Home/Index?Length=4 which doesn't work at all. – Christopher Johnson Aug 29 '13 at 18:55
  • @dombenoit I have the default route that comes stock with a new MVC 4 project before the one I listed below which just contains: Controller, Action and optional id – Christopher Johnson Aug 29 '13 at 18:56
  • @ChristopherJohnson that would do it, the default route will be matched first and the one you want never gets hit. See answer below. – dom Aug 29 '13 at 18:57

1 Answers1

1

It looks as though your Index route is never being hit, probably due to some other route defined before. Always keep your most specific route definitions first and leave the default Controller/Action/Id to be defined last.

routes.MapRoute(
    "Index",
    "{controller}/{action}/{trip}/{year}",
    new { controller = "Home", action = "Index" }
);

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Default", action = "Index", id = UrlParameter.Optional }
);
dom
  • 6,702
  • 3
  • 30
  • 35
  • this worked. Why does removing the parameters from the route change things? – Christopher Johnson Aug 29 '13 at 19:01
  • It doesn't really, what fixed it is swapping the order routes are defined in. Routing goes from top to bottom, whichever route matches first will be the one used. In your case that was the default MVC route instead of your custom one. – dom Aug 29 '13 at 19:03