MVC 路由干扰问题

MVC Routes interference issues

我想要以下内容: Link 到 {controller}/{destination} 和 link 到 {controller}/{action},例如:相应地 flights/berlin 和 flights/search。

我的路由配置如下:

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

如果 "LandPage" 是第一个,路由将始终转到带有 url 参数的着陆页(即 --> flights/search 将转到 flights/index参数目标 = 搜索),这对我不利。 如果 "Default" 是第一个,我尝试导航到 flights/berlin,它将尝试导航到航班控制器和 action = berlin,当然没有这样的操作...

我能想到的唯一解决方案是首先使用 "LandPage",然后将 {destination} 参数与操作名称进行比较并重定向到该操作...我不喜欢该解决方案...任何人都可以考虑其他解决方案吗??

谢谢!

您可以为特定操作设置固定路线:

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

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

在您的默认路线之前。