路段细分参数问题

时间:2013-03-04 20:23:13

标签: asp.net-mvc

我遇到了MVC4中的路由问题。

我有一些生活在特定产品之外的行为以及更多生活在用户所选产品中的行为。为了适应我已经映射了两条路线的行动

        context.MapRoute(
            "CMS_product",
            "CMS/{productId}/{controller}/{action}/{id}",
            new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, productId = default(Guid).ToString(), id = UrlParameter.Optional },
            new string[] { "Areas.CMS.Controllers" }
        );

        context.MapRoute(
            "CMS_default",
            "CMS/{controller}/{action}/{id}",
            new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional },
            new string[] { "Areas.CMS.Controllers" }
        );

因此,虽然这适用于通用,因为我的路线不会再与默认路线匹配,而是获取类似

的网址

〜/ CMS /产品/列表

在产品外部操作时,我会得到这样的网址。

〜/ CMS / 00000000-0000-0000-0000-000000000000 /产品/列表

另一个注意事项:我曾尝试将Prodcut / List硬编码为路径,并将其放在CMS_product之前,希望它能在其他网址之前匹配。我觉得我必须忽略一些简单的事情。

2 个答案:

答案 0 :(得分:1)

为了完整起见,其他人是否遇到类似的问题是解决方案。

       // used to match ~/CMS/00000000-0000-0000-0000-000000000000/Product/List 
       // prevents the GUID.Empty from showing when there is no product value
       // in the segment
       context.MapRoute(
            name: "CMS_nullproduct",
            url: "CMS/{controller}/{action}/{id}",
            defaults: new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional },
            constraints: new { productId = Guid.Empty.ToString() },
            namespaces: new string[] { "Areas.CMS.Controllers" }
        ); 

        // matches any route with a productId segment value of anything aside from
        // GUID.Empty
        context.MapRoute(
            name: "CMS_product",
            url: "CMS/{productId}/{controller}/{action}/{id}",
            defaults: new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional },
            constraints: new { productId = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$" },
            namespaces: new string[] { "Areas.CMS.Controllers" }
        );

        context.MapRoute(
            name: "CMS_default",
            url: "CMS/{controller}/{action}/{id}",
            defaults: new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional },
            namespaces: new string[] { "Areas.CMS.Controllers" }
        );

答案 1 :(得分:0)

在我看来,您应该删除productId的默认值。

context.MapRoute(
        "CMS_product",
        "CMS/{productId}/{controller}/{action}/{id}",
        new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional },
        new string[] { "Areas.CMS.Controllers" }
    );

如果您没有提供productId,您的路由引擎应匹配第二条路线并生成〜/ CMS / Product / List,但如果您提供productId,则它匹配第一条规则。

另外,您可以编写自定义IRouteConstraint或使用正则表达式来限制productId值。

context.MapRoute(
        "CMS_product",
        "CMS/{productId}/{controller}/{action}/{id}",
        new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional },
        new { productId = @"^\d{8}\-\d{4}\-\d{4}\-\d{4}\-\d{12}$" }
        new string[] { "Areas.CMS.Controllers" }
    );
相关问题