【问题标题】:ASP.NET MVC 3.0 Url Routing IssueASP.NET MVC 3.0 Url 路由问题
【发布时间】:2012-01-26 03:55:33
【问题描述】:

您好,我注册的路线如下:-

 routes.MapLocalizedRoute("Category",
                        "{SeName}-c{categoryId}",
                        new { controller = "Catalog", action = "Category", SeName =       UrlParameter.Optional },
                        new { categoryId = @"\d+", SeName = @"([-\w+]+(/[-\w+]+)*)+" },
                        new[] { "Nop.Web.Controllers" });

 routes.MapLocalizedRoute("CategoryWithManufacture",
                   "{SeName}-d{categoryId}/{ManufactureName}/{Color}",
                   new { controller = "Catalog", action = "Category", SeName = UrlParameter.Optional, Color = UrlParameter.Optional },
                   new { categoryId = @"\d+", SeName = @"([-\w+]+(/[-\w+]+)*)+", ManufactureName = @"([-\w+]+(/[-\w+]+)*)+", Color = @"([-\w+]+(/[-\w+]+)*)+" },
                   new[] { "Nop.Web.Controllers" });

我们生成如下:-

 href="@Url.RouteUrl("CategoryWithManufacture", new { categoryId = currentCategoryId, SeName = seName, ManufactureName = manufacturerFilterItem.Name, Color = color })"


 href="@Url.RouteUrl("Category", new { categoryId = currentCategoryId, SeName = seName})"

有一种方法可以直接将值分配给第二个参数“Color”,而无需将值分配给“ManufactureName”。表示:-(使用 CategoryWithManufacture Route)

/gloves-d18/red  (second parameter)
/gloves-d18/hp/red (first and second both)

我已经通过将制造名称和颜色都设为可选来尝试这些,但是当我们将值分配给第一个参数而不是第二个参数时,它可以工作。但是我们不是先给第二个参数赋值,那就不行了。

请给我推荐可用的链接或示例代码。

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-2 razor


    【解决方案1】:

    在路由定义中只有最后一个参数可以是可选的。给定以下 url /gloves-d18/red,路由引擎不可能知道red 在这里指的是{Color} 而不是{ManufactureName}。完成这项工作的唯一方法是为这两个部分编写一些约束。现在它们都具有完全相同的正则表达式约束。

    【讨论】:

    • 请建议我对我的路线进行任何修改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 2011-11-11
    • 2013-11-13
    相关资源
    最近更新 更多