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
?
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
?
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!
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.
UsersController
makes sense if you design your routes like /api/users
(all users) and /api/users/{userId}
(single user) –
Thornie /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 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!
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.
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
© 2022 - 2024 — McMap. All rights reserved.