更改 ASP.NET MVC 中的控制器名称约定 [英] change controller name convention in ASP.NET MVC
问题描述
有没有办法改变 ASP.NET MVC 中控制器的命名约定?
Is there a way to change the naming convention for controllers in ASP.NET MVC?
我想要的是将我的控制器命名为 InicioControlador
而不是 InicioController
,或者更好的是,使用前缀而不是后缀,并具有 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屋!