Step 1:
Create the route constraint.
public class RootRouteConstraint<T> : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
var rootMethodNames = typeof(T).GetMethods().Select(x => x.Name.ToLower());
return rootMethodNames.Contains(values["action"].ToString().ToLower());
}
}
Step 2:
Add a new route mapping above your default mapping that uses the route constraint that we just created. The generic parameter should be the controller class you plan to use as your “Root” controller.
routes.MapRoute(
"Root",
"{action}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional},
new {isMethodInHomeController = new RootRouteConstraint<HomeController>()});
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new
{controller = "Home", action = "Index", id = UrlParameter.Optional}
);
Now you should be able to access your home controller methods like so:
example.com/about,
example.com/contact
This will only affects HomeController. Alll other Controllers will have the default routing functionality.