What you can do is create a custom factory controller. So you can have your own code to decide which controller to call when.
public class CustomControllerFactory : IControllerFactory { #region IControllerFactory Members public IController CreateController(RequestContext requestContext, string controllerName) { if (string.IsNullOrEmpty(controllerName)) throw new ArgumentNullException("controllerName"); //string language = requestContext.HttpContext.Request.Headers["Accept-Language"]; //can be used to translate controller name and get correct controller even when url is in foreign language //format controller name controllerName = String.Format("MyNamespace.Controllers.{0}Controller",controllerName.Replace("-","_")); IController controller = Activator.CreateInstance(Type.GetType(controllerName)) as IController; controller.ActionInvoker = new CustomInvoker(); //only when using custominvoker for actionname rewriting return controller; } public void ReleaseController(IController controller) { if (controller is IDisposable) (controller as IDisposable).Dispose(); else controller = null; } #endregion }
To use this user controller, you must add it to your global.asax file
protected void Application_Start() { RegisterRoutes(RouteTable.Routes); ControllerBuilder.Current.SetControllerFactory(typeof(CustomControllerFactory)); }
Please note that this only works for the controller, not for actions ... To enable custom rewriting of actions before starting them, use this code:
public class CustomInvoker : ControllerActionInvoker { #region IActionInvoker Members public override bool InvokeAction(ControllerContext controllerContext, string actionName) { return base.InvokeAction(controllerContext, actionName.Replace("-", "_")); } #endregion }
I got most of this code from this blog and adjusted it for my needs. In my case, I want the dash to separate the words in the name of my controller, but you cannot create an action with a dash in the name.
Hope this helps!
Peter source share