【问题标题】:MVC URL Routes with 2 or 3 levels具有 2 或 3 级的 MVC URL 路由
【发布时间】:2016-04-09 10:28:56
【问题描述】:

我认为我的路线比他们需要的更先进。 我有一些产品属于几个不同的 3 级类别,但我的大多数产品都属于 2 级类别。没有产品属于 1 级类别。

所以它是这样的:
* 猫/subCat/subSubCat/产品
* 猫/子猫/产品

我想在我的路线中定义我的网址。
2 索引:../Shop/Sortiment/cat/subCat
2 详细信息:../Shop/Sortiment/cat/subCat/product/1/name
3 索引:../Shop/Sortiment/cat/subCat/subSubCat
3 详细信息:../Shop/Sortiment/cat/subCat/subSubCat/product/2/name

routes.MapRoute(
    name: "CategoryIndex",
    url: "Shop/Sortiment/{category}/{subCategory}/{subSubCategory}",
    defaults: new { controller = "Sortiment", action = "Index", subCategory= UrlParameter.Optional, subSubCategory = UrlParameter.Optional }
);
routes.MapRoute(
    name: "ProductDetails",
    url: "Shop/Sortiment/{category}/{subCategory}/{subSubCategory}/product/{id}/{productName}",
    defaults: new { controller = "Sortiment", action = "Details", subSubCategory = UrlParameter.Optional, productName = UrlParameter.Optional }
);

我的产品类有一个Category Category 属性。每个 Category 都有一个 virtual Category ParentCategory 属性,该属性要么为空(第一级类别),要么填充了它的父类别。

使用 2 级产品,我可以编写这样的链接(在我的路线中没有 subSubCategory):

@Url.RouteUrl("ProductDetails", new
{
    category = item.Category.ParentCategory.Name,
    subCategory = item.Category.Name,
    id = item.ID,
    productName = item.Name
})

但是现在如果我有 2 级或 3 级的产品,我想在下面写这个,但是我当然会在 2 级产品上得到 nullrefexception,因为它们没有 2 ParentCategory

@Url.RouteUrl("ProductDetails", new
{
    category = item.Category.ParentCategory.ParentCategory.Name,
    subCategory = item.Category.ParentCategory.Name,
    subSubCategory = item.Category.Name,
    id = item.ID,
    productName = item.Name
})

那么我需要怎么做才能以我想要的方式获取我的 URL?也许对我来说最好重做我的路线?希望我给了你足够的信息。

【问题讨论】:

标签: c# asp.net-mvc routes


【解决方案1】:

您似乎为 2 级 url 使用了错误的路由名称。改为:

@Url.RouteUrl("CategoryIndex", new
{
category = item.Category.ParentCategory.Name,
subCategory = item.Category.Name,
id = item.ID,
productName = item.Name
})

【讨论】:

    【解决方案2】:

    罗伯,

    这是您应该为嵌套路由做的第一件事。

    对于这样的嵌套 url,我认为你应该配置你的路由如下:-

     routes.MapRoute(
                name: "ProductDetails",
                url: "shop/{*subCategory }",
                defaults: new { controller = "Sortiment", action = "Details", subCategory = UrlParameter.Optional }
            );
    

    【讨论】:

      【解决方案3】:

      您可以只继承RouteBase,这样您就可以完全控制您的网址。一种选择是让它们基于 this answer 中的主键使它们由数据库驱动,因此您只需要跟踪 URL 映射的主键即可提取您的产品/类别信息。

      然后,当您指定一个 URL 时,您只需要包含其主键作为 id 路由值、控制器和操作 - 就是这样。

      @Html.Action("Details", "CustomPage", new { id = 1234 })
      

      请注意,您可以有一个产品路线和一个类别路线。

      【讨论】:

        【解决方案4】:
         routes.MapRoute(
                   name: "SubCategoryAction",
                   url: "{action}/{id}/{pid}",
                   defaults: new { controller = "ControllerName", action = "ActionName", id = UrlParameter.Optional ,pid= UrlParameter.Optional }
                   );
        
        //or use traditional AttributeRouting as
        
        [AttributeRouting.Web.Mvc.Route("{action}/{id}/{pid}")]    
        public ActionResult ActionName(string id,string pid){}
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-30
          • 1970-01-01
          • 1970-01-01
          • 2012-07-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多