2008-09-23 65 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的名單。