开发者

Need Help Creating a Route in ASP.NET MVC

I want to create a route like

//Widget开发者_StackOverflow中文版s/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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜