有人在MVC中建立了良好的行动命名约定吗?我特别关注ASP.net MVC,但这是一个普遍的问题。例如,我有一个显示登录屏幕(Login)的操作和一个处理该页面登录请求的操作(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"

会产生(对于论坛)

的网址
* 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风格的命名方案,他解释了一些独特的例外。

Rails为CRUD操作提供了一个很好的操作命名约定: Rails路由来自外面

<代码> HTTP谓词路径控制器#用于的操作 GET /照片#index索引显示所有照片的列表 GET / photos / new photos #new返回用于创建新照片的HTML表单 POST /照片#create创建一张新照片 GET / photos /:id photos#show显示特定照片 GET / photos /:id / edit photos#edit返回一个用于编辑照片的HTML表单 PATCH / PUT /照片/:id照片#update更新特定照片 DELETE / photos /:id photos#destroy删除特定的照片

这实质上是对 Paul Shannon的回答的更新, 因为他的来源(Rob Conery)隐含地说他从Rails复制了他的名单。

内置的Django动作后缀为_done。所以LoginDone将是处理Login的页面(在ASP.NET MVC驼峰案例样式中)。

用于Controller Action命名的惯例是相当无关紧要的,只要它对您来说是一致的并且易于理解。

对于您的登录操作,LoginDone很好,同样是ProcessLogin很容易理解,所以使用您觉得舒服的约定。

就个人而言,我可能会支持Login和ProcessLogin,因为LoginDone可能在Action正在做的事情上有些误导 - 这当然是假设Action对用户的凭据做出反应并检查它们是否有效。然后,一旦登录成功,您就可以通过另一个名为LoginDone的Action,如果不成功,则可以通过LoginFailed。

Stephen Walther关于 ASP.NET MVC提示#11&#8211;使用标准控制器操作名称可能会澄清您有关 MVC Action 命名约定的命名约定...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top