控制器名称是从类名称派生的吗?
这是一个新手问题...
我正在查看默认的 asp.net mvc3 项目,注意到有一个名为:的控制器:
public class AccountController : Controller
我查看了整个代码,找不到指定 AccountController 映射到 /Account/ 的位置网址。
我发现您可以使用 Global.asax
中的 routes.MapRoute(..)
更改路由,但我仍然不知道他们指定 AccountController 映射到的位置/帐户/。
如果从类名中假定它,那么这是否意味着所有控制器类都必须命名为 xxxxxController?
This is a newbie question...
I am looking at the default asp.net mvc3 project and noticed that there is a controller called:
public class AccountController : Controller
I looked throughout the code and couldn't find a place that specified AccountController maps to /Account/ for the URL.
I discovered that you can change the routing using routes.MapRoute(..)
in the Global.asax
, but I still don't know where they specified that AccountController maps to /Account/.
If it is assumed from the class name, then does that mean all controller classes have to be named xxxxxController?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,你是对的,所有控制器都需要遵循结尾
“Controller”
的命名约定。请参阅 CodePlex 上 ASP.NET MVC 代码中的
ControllerName
属性:无论如何,您可以使用自己的控制器工厂来更改命名约定。
希望有帮助。
Yes you are right, all controllers need to follow the naming convention of an ending
"Controller"
.See the
ControllerName
property in the ASP.NET MVC code on CodePlex:Anyhow, you could change the naming convention by using your own controller factory.
Hope that helps.
是的,这是 MVC 框架的一个关键方面,称为 CoC,约定优于配置。这个想法是,只要您愿意遵循类名称、方法名称、文件夹结构等内容的默认约定,您就可以最大限度地减少工作所需的工作量。只有当你想偏离这些惯例时,你才需要付出努力,你当然可以做到。
MVC 框架中有许多这样的项目。除了所有控制器都是名为 XxxxController 的类的约定之外,还有一个约定:所有视图都位于名为 View\Xxxx\Yyyyy.cshtml 的文件夹中。
Yes, this is a key aspect of the MVC framework called CoC, Convention over Configuration. The idea is that, as long as you are willing to follow the default conventions for things like class names, method names, folder structure, etc., you can minimize the amount of work you need to do for things to work. You only put in effort if you want to deviate from those conventions, which you certainly can do.
There are a number of such items in the MVC framework. In addition to the convention that all controllers are classes named XxxxController, there is the convention that all views are found in a folder named View\Xxxx\Yyyyy.cshtml.
是的,确实如此,除非您实现自己的控制器工厂。
Yes it does, unless you implement your own Controller factory.