ASP.NET MVC Controller Naming Pluralization
Asked Answered
S

4

121

RESTful conventions indicate using plural nouns over singular objects.

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

Synthiasyntonic answered 17/9, 2012 at 13:18 Comment(2)
I don't think they are supposed to be pluralized judging from the default ones that come with the MVC tutorials on ASP.net.Petersen
The default ASP.NET Web API for example has a mix of both singular (HomeController, AccountController) and plural names (ValuesController).Sparkman
S
54

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

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

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

Shapeless answered 17/9, 2012 at 15:45 Comment(2)
Thumbs up for emphasis on consistency.Cristiecristin
I'd say that actually, this is the correct answer: https://mcmap.net/q/181056/-asp-net-mvc-controller-naming-pluralization (it has more votes but wasn't marked as "the answer). The AccountController is for the account of the current user; using plural would be very weird unless it is there for managing multiple users in an admin dashboard; but that's a different use case and should (IMHO) be a different controller. And there is also only one Home for the HomeController. So ... no, the consistency should be all about understanding what a controller does and giving it an appropriate name based on that.Tailspin
I
220

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.

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.

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

Incendiary answered 12/8, 2015 at 15:3 Comment(4)
Agreed - For example, a UsersController makes sense if you design your routes like /api/users (all users) and /api/users/{userId} (single user)Thornie
Wouldn't /api/user/{userId} make just as much sense though, you could argue that even in the case of dealing with multiple users, that your controller reflects the entity type, thus HomeController would deal with entities of type Home, hence /Home/{homeId} and /Home/All-Homes/Unlettered
I agree with this explanation.Wakashan
This explanation is "user intuitive". Nice one.Annmarie
S
54

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

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

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

Shapeless answered 17/9, 2012 at 15:45 Comment(2)
Thumbs up for emphasis on consistency.Cristiecristin
I'd say that actually, this is the correct answer: https://mcmap.net/q/181056/-asp-net-mvc-controller-naming-pluralization (it has more votes but wasn't marked as "the answer). The AccountController is for the account of the current user; using plural would be very weird unless it is there for managing multiple users in an admin dashboard; but that's a different use case and should (IMHO) be a different controller. And there is also only one Home for the HomeController. So ... no, the consistency should be all about understanding what a controller does and giving it an appropriate name based on that.Tailspin
I
13

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.

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

Immemorial answered 23/4, 2014 at 14:58 Comment(0)
W
3

It does depend on the context of the controller, and a RESTful API lends itself really well to this format:

GET     Account        gets all accounts
GET     Account/id     gets the account for the specified id
POST    Account        creates an account
PATCH   Account/id     updates an account for the specified id
DELETE  Account/id     deletes an account for the specified id

In most cases it doesn't actually seem to matter if its singular or plural, because the verb and id tell you what's going on... except for the POST... I feel like POST Accounts is ever so slightly misleading in that it might create multiple accounts.

After trialling both ways over the years I prefer the singular by default. I have used Account and Accounts - the accounts was literally for batch operations so I ended up with:

GET     Account        gets all accounts
GET     Account/id     gets the account for the specified id
POST    Account        creates an account
PATCH   Account/id     updates an account for the specified id
DELETE  Account/id     deletes an account for the specified id

POST    Accounts       creates multiple accounts as per POST body
Wetnurse answered 23/3, 2023 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.