The Problem
You want to create urls that look something like http://SomeSite.com/Categories/Industrial/Fasteners/3.8-MM-Titanium-Wrench-Bolts/ so your site can rank extra high when people search for those profitable 3.8 MM Titanium Wrench Bolts. And in natural, RESTful fashion, your site and database is structured so that the Wrench Bolts are in a subcategory of Fasteners, which are part of the Industrial Category
The Cause:
None really, I’m just listing how one does this sort of thing.
The Solution
Firstly you have to define your route in your global.asax file, in the Register Routes section
routes.MapRoute(
“ItemInd”, // Route name
“Categories/{CategoryName}/{SubCategoryName}/{id}”, // URL with parameters
new { controller = “Product”, action = “Details”, CategoryName = “”, SubCategoryName = “”, id = “” } // Parameter defaults
);
a
Then you have to set your Route Values
Here is an ActionLink helper –
@Html.ActionLink(p.Name, “Details”, new { controller = “Product”, action = “Details”, CategoryName = p.SubCategory.Category.Name, SubCategoryName = p.SubCategory.Name, id = p.Name })
And if you want to use the URL.Action helper, here is what that would look like.
<a href=”@Url.Action(“Details”, “Product”, new { controller = “Product”, action = “Details”, CategoryName = p.SubCategory.Category.Name, SubCategoryName = p.SubCategory.Name, id = p.Name })”>Some Text</a>
as you might suspect, the new { controller = “Product”, action = “Details”, CategoryName = p.SubCategory.Category.Name, SubCategoryName = p.SubCategory.Name, id = p.Name } maps exactly to the corresponding parts in the global.asax file.
This wasn’t really a how to fix problem, but really more about using asp.net mvc 3 RouteValues. I’ve found a lack of documentation on how to make relatively complicated urls, so I thought this would be a start!