Eric Langland
Eric Langland

Reputation: 95

ASP.net MVC Route Configuration

I'm new to MVC so I'm having some issues figuring out how to set up my routes for the following scenario.

Let's say I have site that looks up flight data and show three major views for each flight.

I want to have the URL structure as follows:

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

So, basically the lookup key is the first item after the domain. Any subsequent part of the URL maps to a specific view for that flight.

Seems simple but I can't seem to figure out how to structure the controller and routes...

Upvotes: 3

Views: 2915

Answers (1)

Yogiraj
Yogiraj

Reputation: 1982

Try this

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

You should to put this at the top in RegisterRoutes method of RouteConfig.cs (MVC4) or Global.asax (MVC3)

You can read more on routing on asp.net site http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs

Upvotes: 5

Related Questions