You can do it like this:
routes.MapRoute("Default", "{category}/{subcategory}",
new { controller = "CategoryController", action = "Display", id = "" }
);
and then in your controller:
public class CategoryController : Controller
{
public ActionResult Display(string category, string subcategory)
{
// do something here.
}
}
Do not that any the route above will be used for ALL routes (you can't have a About page etc unless you specify explicit routes before the above one).
You could however include a custom constraint to limit the route to only existing categories. Something like:
public class OnlyExistingCategoriesConstraint : IRouteConstraint
{
public bool Match
(
HttpContextBase httpContext,
Route route,
string parameterName,
RouteValueDictionary values,
RouteDirection routeDirection
)
{
var category = route.DataTokens["category"];
//TODO: Look it up in your database etc
// fake that the category exists
return true;
}
}
Which you use in your route like this:
routes.MapRoute("Default",
"{category}/{subcategory}",
new { controller = "CategoryController", action = "Display", id = "" },
new { categoryExists = new OnlyExistingCategoriesConstraint() }
);
In that way it won't interfere with your other defined routes.