I think I have made my routes more advanced than they need to be. I have a few products that are in a few different 3-level categories, but most of my products are in 2-level categories. No products are in a 1-level category.
So its like this:
* cat/subCat/subSubCat/products
* cat/subCat/products
And I'd like to have my URLs like defined in my routes.
2 Index: ../Shop/Sortiment/cat/subCat
2 Details: ../Shop/Sortiment/cat/subCat/product/1/name
3 Index: ../Shop/Sortiment/cat/subCat/subSubCat
3 Details: ../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 }
);
My products class have a Category Category
property. Each Category
Have a virtual Category ParentCategory
property that is either null (first level category) or populated with it's parent category.
With 2-level products I can write links like this (with no subSubCategory
in my routes):
@Url.RouteUrl("ProductDetails", new
{
category = item.Category.ParentCategory.Name,
subCategory = item.Category.Name,
id = item.ID,
productName = item.Name
})
But now If I have products that are either 2 or 3-levels I want to write this below, but ofcourse I get nullrefexception on the 2-level products since they don't have 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
})
So what do I need to do to get my URLs the way I want to? Maybe it's best for me to redo my routes? Hopefully I gave you enough information.