0

I want to have the following: Link to {controller}/{destination} and link to {controller}/{action}, for example: flights/berlin and flights/search accordingly.

My routes config is as follows:

    routes.MapRoute(
     name: "LandPage",
     url: "{controller}/{destination}",
     defaults: new { controller = "Flights", action = "Index", destination = UrlParameter.Optional }
 );

      routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

If "LandPage" is first, the route will go always to the land page with the url parameter (i.e. --> flights/search will go to flights/index with parameter destination = search) and its bad for me. If "Default" will be first, and I try to navigate to flights/berlin, it will try to navigate to the flights controller and action = berlin, of course no such action...

The only solution I can think of is using "LandPage" first, and compare the {destination} parameter with name of action and redirect to that action... I don't like that solution... anyone can think about another solution??

Thanks!

Ziv Weissman
  • 4,400
  • 3
  • 28
  • 61

1 Answers1

1

You can set fixed routs for specific actions:

routes.MapRoute(
    name: "Search",
    url: "Flights/Search/{search}",
    defaults: new { controller = "Flights", action = "Search", search = UrlParameter.Optional }
);

and

routes.MapRoute(
    name: "LandPage",
    url: "Flights/{destination}",
    defaults: new { controller = "Flights", action = "Index", destination = UrlParameter.Optional }
);

before your default route.

Stephen Reindl
  • 5,659
  • 2
  • 34
  • 38
  • Thanks, but its a problem... i have many controllers and actions, so I will need to do too many routes... :/ – Ziv Weissman Mar 08 '15 at 15:50
  • 1
    If you follow the standard rules for controllers/actions for the rest of your controllers there should be no problem. MVC has some basic rules. If you do not follow them, you have to create workarounds. A workaround might be to create the routes dynamically by using http://stackoverflow.com/a/791993/2298807 or reflection – Stephen Reindl Mar 08 '15 at 16:14