2008-09-23 35 views
24

有没有人为MVC中的动作建立了良好的命名约定?我特别关注ASP.net MVC,但这是一个普遍的问题。例如,我有一个动作显示登录屏幕(登录)和一个处理来自该页面的登录请求(LoginTest)。我不喜欢这些名字,而且我还有许多应用可供编写。动作命名约定

回答

39

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 

Rob Conery on RESTful Architecture for MVC

+0

我还看到,使用最新的MVC,您可以设置哪些动词被动作接受,因此您可以为初始页面加载和保存信息命名动作编辑。 – stimms 2008-10-23 01:11:04

+0

似乎来自Rob Conery的帖子早于赋予显示形式和处理形式方法的能力。我还注意到Nerd Dinner示例有Edit,但Controller模板具有用于修改实体的更新。 – Richard 2009-07-19 13:21:07

0

内置的Django动作后缀_done。所以LoginDone将会是处理Login的页面(以ASP.NET MVC骆驼案例风格)。

+0

这是正常情况,但它意味着,登录已经完成时,真的做动作处理登录。也许提示登录和登录。 – stimms 2008-09-23 00:58:41

0

与控制器操作命名使用哪种约定是相当不相干的,只要它对你是一致的,并且可以被那些工作者轻松理解。

在您登录操作的情况下,LoginDone很好,同样的是ProcessLogin会很容易理解,因此请使用您感觉舒适的约定。

就我个人而言,我可能在Login和ProcessLogin方面,因为LoginDone可能会略微误导Action的行为 - 这当然假设Action对用户的凭据作出反应并检查它们是否有效。一旦登录成功,您可以通过另一个称为LoginDone的操作,如果不是,则可以通过LoginFailed。

1

我发现一个blog post by Stephen Walther有用的发现一致命名方案。他也是从REST风格的命名方案衍生出来的,他解释了一些独特的例外。

1

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的名单。