捕捉所有路由
假设我们想要一个允许未绑定数量的段的路由,如下所示:
- http://example.com/Products/ (查看所有产品)
- http://example.com/Products/IT
- http://example.com/Products/IT/Laptops
- http://example.com/Products/IT/Laptops/Ultrabook
- http://example.com/Products/IT/Laptops/Ultrabook/Asus
- 等等
我们需要添加一个路由,通常在路由表的末尾,因为这可能会捕获所有请求,如下所示:
routes.MapRoute("Final", "Route/{*segments}",
new { controller = "Product", action = "View" });
在控制器中,可以处理此问题的操作可能是:
public void ActionResult View(string[] segments /* <- the name of the parameter must match the name of the route parameter */)
{
// use the segments to obtain information about the product or category and produce data to the user
// ...
}