带有可选第一个参数的MVC3路由

时间:2011-10-27 15:38:07

标签: asp.net-mvc-3

我的网站目前设置了以下路线:

        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() }
        );

这会产生类似

的网址
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

我知道我可以创建另外两个路线,与当前的BrandProductsProduct路线相同,并在开头添加额外的“品牌/”。如果需要,我会这样做,但我真的更喜欢不必复制每个路由条目(这不仅仅是这两个)。

有人有建议吗?

1 个答案:

答案 0 :(得分:0)

您可能只想尝试使用Url重写而不是添加指向同一位置的其他路由的复杂性。

对于有效的多个规范格式的URL也不好,一个应该真正301到“正确”的URL。