有没有人为MVC中的动作建立了良好的命名约定?我特别关注ASP.net MVC,但这是一个普遍的问题。例如,我有一个动作显示登录屏幕(登录)和一个处理来自该页面的登录请求(LoginTest)。我不喜欢这些名字,而且我还有许多应用可供编写。动作命名约定
动作命名约定
回答
MS的Rob Conery为操作提出了一些有用的REST风格命名。
* Index - the main "landing" page. This is also the default endpoint. * List - a list of whatever "thing" you're showing them - like a list of Products. * Show - a particular item of whatever "thing" you're showing them (like a Product) * Edit - an edit page for the "thing" * New - a create page for the "thing" * Create - creates a new "thing" (and saves it if you're using a DB) * Update - updates the "thing" * Delete - deletes the "thing"
导致沿线的(对于论坛)的URL
* http://mysite/forum/group/list - shows all the groups in my forum * http://mysite/forum/forums/show/1 - shows all the topics in forum id=1 * http://mysite/forums/topic/show/20 - shows all the posts for topic id=20
内置的Django动作后缀_done。所以LoginDone将会是处理Login的页面(以ASP.NET MVC骆驼案例风格)。
这是正常情况,但它意味着,登录已经完成时,真的做动作处理登录。也许提示登录和登录。 – stimms 2008-09-23 00:58:41
与控制器操作命名使用哪种约定是相当不相干的,只要它对你是一致的,并且可以被那些工作者轻松理解。
在您登录操作的情况下,LoginDone很好,同样的是ProcessLogin会很容易理解,因此请使用您感觉舒适的约定。
就我个人而言,我可能在Login和ProcessLogin方面,因为LoginDone可能会略微误导Action的行为 - 这当然假设Action对用户的凭据作出反应并检查它们是否有效。一旦登录成功,您可以通过另一个称为LoginDone的操作,如果不是,则可以通过LoginFailed。
我发现一个blog post by Stephen Walther有用的发现一致命名方案。他也是从REST风格的命名方案衍生出来的,他解释了一些独特的例外。
Rails对CRUD操作有一个很好的动作命名约定:Rails Routing from the Outside In。
HTTP Verb Path Controller#Action Used for GET /photos photos#index display a list of all photos GET /photos/new photos#new return an HTML form for creating a new photo POST /photos photos#create create a new photo GET /photos/:id photos#show display a specific photo GET /photos/:id/edit photos#edit return an HTML form for editing a photo PATCH/PUT /photos/:id photos#update update a specific photo DELETE /photos/:id photos#destroy delete a specific photo
这实质上是一个更新Paul Shannon's answer, 因为他的来源(罗布科纳)暗示说,他复制了他从Rails的名单。
斯蒂芬·瓦尔特对ASP.NET MVC Tip #11 – Use Standard Controller Action Names后可能会澄清你关于命名MVC Action
命名约定的约定......
- 1. Grails动作的命名约定
- 2. JSON动作的MVC命名约定
- 3. 工作项命名约定
- 4. 命名约定
- 5. 命名约定
- 6. TSQL命名约定〜这个命名约定叫什么名字?
- 7. c#命名约定
- 8. Linq2sql命名约定
- 9. PHP命名约定?
- 10. serialVersionUID命名约定
- 11. SQL命名约定
- 12. Oracle命名约定
- 13. 表命名约定?
- 14. MvvmCross命名约定
- 15. MySQL命名约定
- 16. Python命名约定
- 17. 头命名约定
- 18. xmlns命名约定
- 19. Ruby命名约定?
- 20. BEM:命名约定
- 21. Protobuf命名约定
- 22. .NET命名约定
- 23. FFMPEG命名约定
- 24. 命名约定Rails
- 25. CodeRush命名约定
- 26. Microsoft命名约定
- 27. 命名约定DependencyProperty.RegisterAttached
- 28. DAO命名约定
- 29. mysql命名约定
- 30. URL命名约定
我还看到,使用最新的MVC,您可以设置哪些动词被动作接受,因此您可以为初始页面加载和保存信息命名动作编辑。 – stimms 2008-10-23 01:11:04
似乎来自Rob Conery的帖子早于赋予显示形式和处理形式方法的能力。我还注意到Nerd Dinner示例有Edit,但Controller模板具有用于修改实体的更新。 – Richard 2009-07-19 13:21:07