Search code examples
asp.net-mvccontrollerprefix

change controller name convention in ASP.NET MVC


Is there a way to change the naming convention for controllers in ASP.NET MVC?

What I want is to name my controllers InicioControlador instead of InicioController, or better yet, use a prefix instead of a suffix, and have ControladorInicio as my controller name.

From what I have read so far, I think I have to implement my own Controller Factory. I would be very grateful if any of you could point me in the right direction.


Solution

  • Yes, ControllerFactory is the best solution of your problem.

    public IController CreateController(RequestContext requestContext, string controllerName)
        {            
            BaseController controller;
    
            switch (controllerName.ToLower())
            {
                case "product": case "products": controller = new MyProductController(); break;
                case "home": controller = new MyHomeController(); break;
                case "account": controller = new MyAccountController(); break;
                default: controller = null; break;
            }
    
            return controller;
        }
    

    This is my first ControllerFactory - but it is very stupid :) You must use reflection and avoid this ugly switch.