Ad

Can I Generate ASP.NET MVC Routes From A Sitemap?

- 1 answer

I'm thinking of learning the ASP.NET MVC framework for an upcoming project. Can I use the advanced routing to create long URLs based on the sitemap hierarchy?

Example navigation path:

Home > Shop > Products > Household > Kitchen > Cookware > Cooksets > Nonstick

Typical (I think) MVC URL:
http://example.com/products/category/NonstickCooksets

Desired URL:
http://example.com/shop/products/household/kitchen/cookware/cooksets/nonstick

Can I do this?

Ad

Answer

Zack, if I understand right you want unlimited depth of the subcategories. No biggie, since MVC Preview 3 (I think 3 or 4) this has been solved.

Just define a route like

"{controller}/{action}/{*categoryPath}"

for an url such as :

http://example.com/shop/products/household/kitchen/cookware/cooksets/nonstick

you should have a ShopController with a Products action :

public class ShopController : Controller
{
...
    public ActionResult Products(string categoryPath)
    {
        // the categoryPath value would be
        // "household/kitchen/cookware/cooksets/nonstick". Process it (for ex. split it)
        // and then decide what you do..
        return View();
    }
Ad
source: stackoverflow.com
Ad