更改 ASP.NET MVC 中的控制器名称约定

发布于 2024-09-04 15:36:19 字数 253 浏览 9 评论 0原文

有没有办法更改 ASP.NET MVC 中控制器的命名约定?

我想要的是将我的控制器命名为 InicioControlador 而不是 InicioController,或者更好的是,使用前缀而不是后缀,并将 ControladorInicio 作为我的控制器名称。

从我到目前为止所读到的内容来看,我认为我必须实现自己的控制器工厂。如果你们中有人能指出我正确的方向,我将非常感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坦然微笑 2024-09-11 15:36:19

在搜索 MVC 源代码后,我决定更深入地挖掘,并找到了我正在寻找的东西。控制器名称的约定深深植根于 MVC 框架的根源,特别是在 ControllerDescriptorControllerTypeCache 两个类中。

ControllerDescriptor 中,它由以下属性给出:

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

ControllerTypeCache 中,它由以下方法给出:

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

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);
      }
    }
  }
甚是思念 2024-09-11 15:36:19

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

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 - 但它非常愚蠢:) 你必须使用反射并避免这种丑陋的开关。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文