ASP.net MVC路由配置

时间:2012-11-29 20:04:38

标签: asp.net-mvc

我是MVC的新手所以我在解决如何为以下场景设置路线方面遇到了一些问题。

假设我有查找航班数据的网站,并显示每个航班的三个主要视图。

我希望网址结构如下:

www.domain.com/<flightnumber>/   <-- Main page for the flight
www.domain.com/<flightnumber>/crew  <-- A page with details of the crew for that flight
www.domain.com/<flightnumber>/destination  <-- details of the destination for the flight

因此,基本上查找键是域之后的第一个项目。 URL的任何后续部分都映射到该航班的特定视图。

看起来很简单,但我似乎无法弄清楚如何构建控制器和路由......

1 个答案:

答案 0 :(得分:5)

试试这个

 routes.MapRoute(
         "YourRouteName", // Route name
         "{flightNumber}/{action}", // URL with parameters
         new { controller = "YourController", action = "YourDefaultAction", flightNumber = 0    } // Parameter defaults
        );

你应该把它放在RouteConfig.cs(MVC4)或Global.asax(MVC3)的RegisterRoutes方法的顶部

您可以在asp.net网站http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs

上阅读有关路由的更多信息
相关问题