asp.net-mvc ASP.NET MVC 控制器命名多元化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12460060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:15:06  来源:igfitidea点击:

ASP.NET MVC Controller Naming Pluralization

asp.net-mvcnaming-conventionspluralizeasp.net-mvc-controller

提问by Petrus Theron

RESTful conventions indicate using plural nouns over singular objects.

RESTful 约定表示在单数对象上使用复数名词。

What is the pluralization convention for naming ASP.NET MVC controllers, i.e.
ProductControlleror ProductsController?

命名 ASP.NET MVC 控制器的复数约定是什么,即
ProductControllerProductsController

采纳答案by Cybermaxs

Some MVC Frameworks use plurals, however the MVC project templates contains a controller called AccountController thus suggesting singlular naming.

一些 MVC 框架使用复数,但是 MVC 项目模板包含一个名为 AccountController 的控制器,因此建议使用单数命名。

It doesn't matter. As with most things in the Asp.net MVC framework the choice is yours. There is no real conventions.

没关系。与 Asp.net MVC 框架中的大多数内容一样,选择权在您手中。没有真正的约定。

It's my personal opinion but what matters is that you pick a scheme and be consistent!

这是我个人的意见,但重要的是你选择一个方案并保持一致!

回答by LouD

I'm going to have to disagree with the previous answers of using either plural or singular and being consistent. Each controller should use a different convention based on whether they interact with single or multiple entities. Especially since the controller name is used by default in the URL.

我将不得不不同意先前使用复数或单数并保持一致的答案。每个控制器应根据它们与单个或多个实体交互使用不同的约定。特别是因为 URL 中默认使用控制器名称。

While the project templates use singular (HomeController, AccountController), there is only one Home and the Account actions only operate on the single account for the session. I would not expect the URLs of /homes to access the homepage, nor would I expect to go to /accounts to manage my account settings.

虽然项目模板使用单数(HomeController、AccountController),但只有一个 Home,并且 Account 操作仅对会话的单个帐户进行操作。我不希望 /homes 的 URL 访问主页,也不希望转到 /accounts 来管理我的帐户设置。

The plural HomesController wouldwork for a real estate website that had methods related to listing and searching multiple homes: /homes/new-listings.

复数 HomesController适用于具有与列出和搜索多个房屋相关的方法的房地产网站:/homes/new-listings。

回答by RitchieD

When you add a controller using MVC scaffolding for an Entity Framework entity, VS2013 makes the controller name plural, so I would suggest using that default which makes controllers for entities plural.

当您使用 MVC 脚手架为实体框架实体添加控制器时,VS2013 将控制器名称设为复数,因此我建议使用该默认值,使实体的控制器变为复数。

UPDATE: I changed my mind. LouD is correct. It depends on the context of the controller.

更新:我改变主意了。LouD 是正确的。这取决于控制器的上下文。