29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

new { controller = "Home", action = "Index" });<br />

}<br />

}<br />

}<br />

routes.MapRoute("", "Public/{controller}/{action}",<br />

new { controller = "Home", action = "Index" });<br />

This new pattern will match only URLs that contain three segments, the first of which must be Public. The other two<br />

segments can contain any value, and will be used for the controller and action variables. If the last two segments are<br />

omitted, then the default values will be used.<br />

I can also create URL patterns that have segments containing both static and variable elements, such as the one shown in Listing<br />

15-12.<br />

Listing 15-12. A URL Pattern with a Mixed Segment in the RouteConfig.cs File<br />

using System;<br />

using System.Collections.Generic;<br />

using System.Linq;<br />

using System.Web;<br />

using System.Web.Mvc;<br />

using System.Web.Routing;<br />

namespace UrlsAndRoutes {<br />

public class RouteConfig {<br />

public static void RegisterRoutes(RouteCollection routes) {<br />

routes.MapRoute("", "X{controller}/{action}");<br />

routes.MapRoute("MyRoute", "{controller}/{action}",<br />

new { controller = "Home", action = "Index" });<br />

}<br />

}<br />

}<br />

routes.MapRoute("", "Public/{controller}/{action}",<br />

new { controller = "Home", action = "Index" });<br />

The pattern in this route matches any two-segment URL where the first segment starts with the letter X. The value for<br />

controller is taken from the first segment, excluding the X. The action value is taken from the second segment. You<br />

can see the effect of this route if you start the application and navigate to /XHome/Index, the result of which is illustrated by<br />

Figure 15-6.<br />

Figure 15-6. Mixing static and variable elements in a single segment<br />

ROUTE ORDERING<br />

373

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!