我想要一個包含兩個可選參數的Route;我以爲以下將工作:帶有可選參數的ASP.NET MVC自定義路由
routes.MapRoute(
"ProductForm",
"products/{action}/{vendor_id}_{category_id}",
new { controller = "Products", action = "Index", vendor_id = "", category_id = "" },
new { action = @"Create|Edit" }
);
但如果同時提供了vendor_id
和category_id
它只能;使用RouteDebug
我看到/products/create/_3
不會觸發我的路線,所以我加了其他兩條路線:
routes.MapRoute(
"ProductForm1",
"{controller}/{action}/_{category_id}",
new { controller = "Home", action = "Index", category_id = "" },
new { controller = "Products", action = @"Create|Edit" }
);
routes.MapRoute(
"ProductForm2",
"{controller}/{action}/{vendor_id}_",
new { controller = "Home", action = "Index", vendor_id = "" },
new { controller = "Products", action = @"Create|Edit" }
);
所以,問題:
使用三個途徑,使路線的唯一途徑與可選參數?
這些URL是否正確,也就是說,你會建議一個更好的方法來做到這一點?