有沒有人爲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. JSON動作的MVC命名約定
- 2. 工作項命名約定
- 3. 命名約定
- 4. 命名約定
- 5. TSQL命名約定〜這個命名約定叫什麼名字?
- 6. Protobuf命名約定
- 7. c#命名約定
- 8. BEM:命名約定
- 9. 表命名約定?
- 10. ç命名約定
- 11. DAO命名約定
- 12. mysql命名約定
- 13. MySQL命名約定
- 14. .NET命名約定
- 15. Linq2sql命名約定
- 16. xmlns命名約定
- 17. FFMPEG命名約定
- 18. PHP命名約定?
- 19. Microsoft命名約定
- 20. URL命名約定
- 21. serialVersionUID命名約定
- 22. 命名約定Rails
- 23. WebApi控制器操作命名約定
- 24. 自動UI測試命名約定
- 25. 良好命名空間命名約定
- 26. 命名空間命名約定
- 27. XAML命名空間命名約定
- 28. 命名約定和命名空間
- 29. MVC5中的命名約定
- 30. Biztalk項目命名約定
我還看到,使用最新的MVC,您可以設置哪些動詞被動作接受,因此您可以爲初始頁面加載和保存信息命名動作編輯。 – stimms 2008-10-23 01:11:04
似乎來自Rob Conery的帖子早於賦予顯示形式和處理形式方法的能力。我還注意到Nerd Dinner示例有Edit,但Controller模板具有用於修改實體的更新。 – Richard 2009-07-19 13:21:07