views:

42

answers:

4

My website uses categories and sub-categories.

I'd like the follow mapping:

/Category/Fruit

/Category/Fruit/Apples

But if I use the below:

routes.MapRoute( 
            "Category", // Route name 
            "Category/{category}/{subcategory}", // URL with parameters 
            new { controller = "Entity", action = "Category" } // Parameter defaults 
        );

I get a 404 for /Category/Fruit however /Category/Fruit/Apples works ok. I'd like /Category/Fruit to work as well but I can't add another route with the same name. How do I get around this?

Thanks in advance.

A: 

Name is not required for routing rules. You can set it to null.

You can describe your routing with the only rule if you'll mark second parameter as optional. Or set second parameter default value as :zerkms suggests

Andrew Florko
>> "if you'll mark second parameter as optional" -- how this can be done if not by setting the default value?
zerkms
It's terminology question : secondParam = UrlParameter.Optional (as PieterG advised)
Andrew Florko
+1  A: 

Specify default value for subcategory

routes.MapRoute( 
        "Category", // Route name 
        "Category/{category}/{subcategory}", // URL with parameters 
        new { controller = "Entity", action = "Category", subcategory = "Some value" } // Parameter defaults 
    );
zerkms
Thanks that works great!
creativeincode
A: 

Phil Haack has a route debugger on his blog.

This utility displays the route data pulled from the request of the current request in the address bar. So you can type in various URLs in the address bar to see which route matches

amexn
A: 

This will work for your scenario

routes.MapRoute(
    "Default", // Route name
    "Category/{category}/{subcategory}/{id}", // URL with parameters
    new { controller = "fruit", action = "apples", id = UrlParameter.Optional } // Parameter defaults
);

and will respond to the url http://.../Category/Fruit/Apples/

PieterG