Create your own RouteHandler. I don't know if this is the best solution though.
public class RemoveDashRouteHandler : MvcRouteHandler
{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
requestContext.RouteData.Values["controller"] = ((string)requestContext.RouteData.Values["controller"]).Replace("-", String.Empty);
return base.GetHttpHandler(requestContext);
}
}
Usage
routes.MapRoute(
name: "AllInOne",
url: "{controller}",
defaults: new { controller = "Default", action = "GetData" }
).RouteHandler = new RemoveDashRouteHandler();
Edit for alternative solution
I found a better solution (in my opinion) by sub-classing Route then overriding GetRouteData. It's better since Route's responsibility is to generate the RouteData while MvcRouteHandler's responsibility is to get the IHttpHandler.
public class RemoveDashRoute : Route
{
private const string ControllerKey = "controller";
public RemoveDashRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints = null, RouteValueDictionary dataTokens = null, IRouteHandler routeHandler = null)
: base(url, defaults, constraints ?? new RouteValueDictionary(), dataTokens ?? new RouteValueDictionary(), routeHandler ?? new MvcRouteHandler())
{
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var routeData = base.GetRouteData(httpContext);
if (routeData != null && routeData.Values.ContainsKey(ControllerKey))
{
routeData.Values[ControllerKey] = ((string)routeData.Values[ControllerKey]).Replace("-", String.Empty);
}
return routeData;
}
}
Usage
routes.Add("AllInOne", new RemoveDashRoute(
url: "{controller}",
defaults: new RouteValueDictionary(new { controller = "Home", action = "GetData" }))
);