ASP .NET MVC 3 是否存在命名约定?
当我学习 MVC 3 时,很明显命名对于让数据库-模型-控制器-视图一起对话至关重要。
是否存在应在 MVC 应用程序中使用的现有命名约定列表?
类似于:
- 表名称(复数)
- 模型名称(表名称的单数)
- 视图文件夹必须与控制器类同名(即 Contract -> 派生自 ContractController)
As I am learning MVC 3 it has become apparent that naming is critical to getting the Dbase-Model-Controller-Views to talk together.
Is there an already existing list of naming conventions that should be used in a MVC application?
Something like:
- Table names (plural)
- Model names (singular of Table Name)
- View folder must be same name as controller class (ie Contract -> derived from ContractController)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
唯一的主要约定是控制器类名称必须以“Controller”结尾。任何其他约定仅代表最佳实践,并且不需要您的应用程序运行。
您提到的表和模型命名约定之所以存在,是因为它们使代码“阅读”得更好(
Select * From products wherecategory = 1
;Products.Insert(new Product())
code>)MVC 自动查找与操作方法名称匹配的视图,并开始在与控制器同名的文件夹中查找。但是,您可以通过在视图结果中指定视图名称来轻松解决这一问题(
return View("MyCustomName")
)The only major convention is that controller class names must end with "Controller". Any other conventions simply represent best practices and are not required for your application to work.
The table and model naming conventions you mentioned are there because they make the code "read" better (
Select * From products where category = 1
;Products.Insert(new Product())
)MVC automatically looks for a view that matches the name of the action method and it starts looking in a folder that has the same name as the controller. However you can easily trump that by specifying the view name in your view result (
return View("MyCustomName")
)没有太多名称约定会破坏您的应用程序,除了少数控制器以“Controller”结尾以及从操作名称调用“View()”的名称推断之外。
MVC 基于约定优于配置的思想,所以我慢慢发现,有很多约定可以遵循,但我还没有找到一个列表。它们很微妙而且非常方便……通常是这样。
Not many name conventions exist that will break your application, other than the few such having your controllers end with "Controller" and name inference of the "View()" call from the action name.
MVC is based on the ideology of convention over configuration, so as I found out slowly, there are many conventions that are useful to follow, I have yet to come across a list though. They are subtle and very convenient... usually.