4

因此,MCV3 的工作方式是您定义通过路由调用的控制器。该控制器是“myControllerName”+“控制器”。

现在该类已经在一个名为控制器的文件夹中。我不希望我的班级名称末尾有那种装饰。它并没有真正做出任何贡献。

我想知道如何改变它并了解更多关于 MVC3 内部发生的事情。

4

3 回答 3

5

简短的回答是:不要打扰。这是按约定配置(也称为约定优于配置,CoC),如果您顺其自然,让 MVC 为所欲为,您将过上更快乐、更健康、无压力的生活。

更长的答案是您将自定义控制器工厂注入 MVC 管道以覆盖默认控制器工厂。可以在本文中找到该过程的完整描述,但本质上,您:

  1. 实施IControllerFactory以做任何你想做的事。
  2. 在您的Application_Start事件中,调用ControllerBuilder.Current.SetControllerFactory以用您自己的控制器工厂替换默认控制器工厂。

MVC 是一个高度可扩展的框架;尽管大多数组件的默认行为可能满足您的需求,但您几乎总是可以将内置对象替换为您自己的。对于您可以覆盖的其他内容,请参阅MVC3 的 13 个可扩展点的列表。

另外,我一开始忘了提,但如果你真的对 MVC 的内容感兴趣,整个框架都是开源的(它在 APL 下),你可以在 codeplex 上找到它。

于 2012-05-27T03:19:50.190 回答
0

控制器后缀被认为是 MVC 的约定(有关约定优于配置方法的一些详细信息,请参见此处http://en.wikipedia.org/wiki/Convention_over_configuration),它具有价值,因为它为所有 MVC 项目提供了一致的结构。此外,它还描述了 MVC 模式中类的功能。

MVC 非常注重约定,如果你想要更多关于配置的东西,可以看看 ASP.Net webforms

于 2012-05-27T03:12:14.347 回答
0

文件夹名称仅对作为人类的您有用。编译器不在乎,它们都被编译成一个 DLL,并且文件夹是无关紧要的(尽管默认情况下它确实被放入带有“控制器”的命名空间中)。该文件夹根本不会上传到最终站点。

Because the folder doesn't exist on the server, you can't make the framework assume that because it's in the Controllers folder it's a controller. It just doesn't work that way, nor is there any way to make it work that way (again, because the Controllers folder does not exist on the server).

The convention of naming things xxxController is to make things easier. If you don't want to follow the conventsion MVC dictates, then perhaps MVC isn't the right framework for you. It will only make your life more difficult if you want to fight the flow.

It's like complaining that you don't like naming your C Sharp files with a .cs extension, or that you don't like naming your Webforms forms with an .aspx.

于 2012-05-27T04:20:51.033 回答