change controller name convention in ASP.NET MVC

后端 未结 2 1530
北恋
北恋 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.

    0 讨论(0)
  • 2020-12-01 22:28

    I decided to dig a bit deeper and found exactly what I was looking for after searching through the MVC source code. The convention for controller names is deep inside the roots of the MVC Framework, especifically in two classes ControllerDescriptor and ControllerTypeCache.

    In ControllerDescriptor it is given by the following attribute:

    public virtual string ControllerName {
      get {
        string typeName = ControllerType.Name;
        if (typeName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)) {
          return typeName.Substring(0, typeName.Length - "Controller".Length);
        }
        return typeName;
      }
    }
    

    In ControllerTypeCache it is given by the following methods:

    internal static bool IsControllerType(Type t) {
      return
        t != null &&
        t.IsPublic &&
        t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) &&
        !t.IsAbstract &&
        typeof(IController).IsAssignableFrom(t);
    }
    
    public void EnsureInitialized(IBuildManager buildManager)
    {
      if (_cache == null)
      {
        lock (_lockObj)
        {
          if (_cache == null)
          {
            List<Type> controllerTypes = TypeCacheUtil.GetFilteredTypesFromAssemblies(_typeCacheName, IsControllerType, buildManager);
            var groupedByName = controllerTypes.GroupBy(
                t => t.Name.Substring(0, t.Name.Length - "Controller".Length),
                StringComparer.OrdinalIgnoreCase);
            _cache = groupedByName.ToDictionary(
                g => g.Key,
                g => g.ToLookup(t => t.Namespace ?? String.Empty, StringComparer.OrdinalIgnoreCase),
                StringComparer.OrdinalIgnoreCase);
          }
        }
      }
    
    0 讨论(0)
提交回复
热议问题