在ASP.NET MVC变化控制器名称约定 [英] change controller name convention in ASP.NET MVC

查看:227
本文介绍了在ASP.NET MVC变化控制器名称约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来改变控制器的命名约定在ASP.NET MVC?

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

我要的是名字我控制器 InicioControlador 而不是 InicioController ,或者更好的是,使用preFIX,而不是一个后缀,并有 ControladorInicio 我的控制器的名称。

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.

推荐答案

是的,是的ControllerFactory你的问题的最佳解决方案。

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;
    }

这是我第一次的ControllerFactory - 但它是非常愚蠢的:)你必须使用反射,避免这种丑陋的开关

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

这篇关于在ASP.NET MVC变化控制器名称约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆