If you want to limit yourself to only on possible controller you can do this:
routes.MapRoute(
"Default", // Route name
"{category}/{action}/{id}", // URL with parameters
new { controller = "Categories", category="DefaultCategory", action = "Index", id = UrlParameter.Optional }
);
If you do it this way you can address different controllers:
routes.MapRoute(
"Default", // Route name
"{controller}/{category}/{id}/{action}", // URL with parameters
new { controller = "Home", category="DefaultCategory", action = "Index", id = UrlParameter.Optional }
);
Based on your comment you would have do go in this direction:
routes.MapRoute(
"Categories",
"{category}/{action}",
new { controller = "Categories", action = "Index" },
new
{
category=
new FromValuesListConstraint("Category1", "Category2", "Category3", "Category4", "Category5")
}
);
// all your default routing
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
FromValuesListContraint is only a sample for the simplest case if you have only a few categories. If you have dynamic categories that come out of a database you have to do a different implementation, but still can follow my example with IRoutecontraints.
Here is the Sample IRouteConstraints Implementation (FromValuesListConstraint):
public class FromValuesListConstraint : IRouteConstraint
{
private List<string>_values;
public FromValuesListConstraint(params string[] values)
{
this._values = values.Select(x => x.ToLower()).ToList();
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
string value = values[parameterName].ToString();
return _values.Contains(value.ToLower());
}
}
The reason why you have to do this whole IRoutConstraint thing here is otherwise you would not be able to use you default route because a request for www.mysite.com/mycontroller or www.mysite.com/mycontroller/myaction would match the categories route.