change controller name convention in ASP.NET MVC

后端 未结 2 1543
北恋
北恋 2020-12-01 21:44

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

2条回答
  •  难免孤独
    2020-12-01 22:24

    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.

提交回复
热议问题