动作命名约定

时间:2020-03-06 14:34:27  来源:igfitidea点击:

是否有人为MVC中的动作建立了良好的命名约定?我专门在看ASP.net MVC,但这是一个普遍的问题。例如,我有一个操作,显示登录屏幕(登录),并处理该页面的登录请求(LoginTest)。我不喜欢这些名称,我还有很多申请书要写。

解决方案

内置的Django操作后缀_done。因此,LoginDone将是处理登录的页面(在ASP.NET MVC驼峰式的情况下)。

只要它对我们而言是一致的,并且对其进行操作的人员容易理解,则对于Controller Action命名使用哪种约定就无关紧要。

对于登录操作,LoginDone很好,而ProcessLogin则易于理解,因此请使用我们习惯的约定。

就我个人而言,我可能会支持Login和ProcessLogin,因为就Action所做的操作而言,LoginDone可能会稍有误导,这当然是假定该Action对用户的凭据做出反应并检查它们是否有效。登录成功后,我们可以传递到另一个名为LoginDone的操作;否则,我们可以传递到LoginFailed。

MS的Rob Conery建议使用一些有用的RESTful样式命名动作。

* 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谈MVC的RESTful架构

我发现Stephen Walther的博客文章对于找到一致的命名方案很有用。他还来自REST风格的命名方案,他解释了一些独特的例外。