更改 ASP.NET MVC 中的控制器名称约定
有没有办法更改 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在搜索 MVC 源代码后,我决定更深入地挖掘,并找到了我正在寻找的东西。控制器名称的约定深深植根于 MVC 框架的根源,特别是在
ControllerDescriptor
和ControllerTypeCache
两个类中。在
ControllerDescriptor
中,它由以下属性给出:在
ControllerTypeCache
中,它由以下方法给出: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
andControllerTypeCache
.In
ControllerDescriptor
it is given by the following attribute:In
ControllerTypeCache
it is given by the following methods:是的,ControllerFactory 是解决您问题的最佳方案。
这是我的第一个 ControllerFactory - 但它非常愚蠢:) 你必须使用反射并避免这种丑陋的开关。
Yes, ControllerFactory is the best solution of your problem.
This is my first ControllerFactory - but it is very stupid :) You must use reflection and avoid this ugly switch.