views:

44

answers:

1

I want to create a route like

//Widgets/PerformanceTable/['best' or 'worst' to sort by performance of an investment]

where either 'best' or 'worst' are required.

Can somebody show me a good way to do this?

Thanks

+4  A: 

I'm going to make the assumption that your controller action has the following signature:

 public ActionResult PerformanceTable(string order)

In which case the following route will work for you:

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{order}", // URL with parameters
            new { controller = "Widgets", action = "PerformanceTable", order = "best" }, // Parameter defaults
            new { order = "(best|worst)" });  // Constraints

If no order is given, a default order of 'best' is passed into the controller.

The final parameter of MapRoute is a regular expression defining the possible values for the order parameter (in this case 'best' and 'worst'). If any other value is given, then the route won't match.

richeym
I like the constraint. I was wondering about how to prevent the route from matching other, similarly generic routes.
Robert Harvey