捕捉所有路由
假設我們想要一個允許未繫結數量的段的路由,如下所示:
- 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
// ...
}