Asp.net mvc 3 具有可选第一个参数的MVC3路由
我的站点当前设置了以下路径:Asp.net mvc 3 具有可选第一个参数的MVC3路由,asp.net-mvc-3,Asp.net Mvc 3,我的站点当前设置了以下路径: routes.MapRoute( "BrandList", "Brands", new { controller = "Brand", action = "Index" } ); routes.MapRoute( "BrandProducts", "{brand}/Products",
routes.MapRoute(
"BrandList",
"Brands",
new { controller = "Brand", action = "Index" }
);
routes.MapRoute(
"BrandProducts",
"{brand}/Products",
new { controller = "Brand", action = "Products", manufacturer = "" },
new { manufacturer = new BrandConstraint() }
);
routes.MapRoute(
"Product",
"{brand}/{partNumber}",
new { controller = "Product", action = "Details", brand = "" },
new { manufacturer = new BrandConstraint() }
);
这会产生如下URL
http://oursite/Brands -> List of all brands
http://oursite/SomeBrand -> List of one brand's products
http://oursite/SomeBrand/ProductA -> Details for product
不过,我刚收到指令,我们现在需要在上面提供相同的页面
http://oursite/Brands -> List of all brands
http://oursite/SomeBrand -> List of one brand's products
http://oursite/Brands/SomeBrand -> List of one brand's products
http://oursite/SomeBrand/ProductA -> Details for product
http://oursite/Brands/SomeBrand/ProductA -> Details for product
我知道我可以再创建两条路线,与当前的BrandProducts
和Product
路线相同,在开始时添加额外的“Brands/”。如果需要,我会这样做,但我真的更希望不必复制每个路线入口(不止这两个)
有人有什么建议吗 您可能只想尝试使用Url重写,而不是增加指向同一位置的其他路由的复杂性 此外,拥有多个有效的URL规范格式也不好,一个URL应该真正指向“正确”的URL