Question

Has anybody established a good naming convention for action in MVC? I was specifically looking at ASP.net MVC but it is a general question. For instance I have an action which displays the login screen (Login) and one which process the login request from that page (LoginTest). I'm not keen on the names and I have a lot of the applicaiton left to write.

Was it helpful?

Solution

Rob Conery at MS suggested some useful RESTful style naming for actions.

* 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"

results in URLs along the lines of (for a forum)

* 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 on RESTful Architecture for MVC

OTHER TIPS

I've found a blog post by Stephen Walther useful for finding a consistent naming scheme. His are also derived from a REST-style naming scheme, with some unique exceptions that he explains.

Rails has a nice action naming convention for CRUD operations: Rails Routing from the Outside In.

HTTP Verb Path Controller#Action Used for GET /photos photos#index display a list of all photos GET /photos/new photos#new return an HTML form for creating a new photo POST /photos photos#create create a new photo GET /photos/:id photos#show display a specific photo GET /photos/:id/edit photos#edit return an HTML form for editing a photo PATCH/PUT /photos/:id photos#update update a specific photo DELETE /photos/:id photos#destroy delete a specific photo

This is essentially an update to Paul Shannon's answer, since his source (Rob Conery) implicitly says that he copied his list from Rails.

The builtin Django actions suffix _done. So LoginDone would be the page that processes Login (in ASP.NET MVC camel case style).

It's fairly irrelevant which convention you use for the Controller Action naming, as long as it's consistant for you and easily understood by those working on it.

In the case of your login Actions, LoginDone is fine and in the same was ProcessLogin will is easy to understand, so use a convention that you feel comfortable with.

Personally I would probably side with Login and ProcessLogin, as LoginDone is probably slightly misleading in terms of what the Action is doing - this is of course assuming that the Action is reacting to the users' credentials and checking whether they are valid. You could then pass through to another Action called LoginDone once the login is successful, or LoginFailed if it's not.

Stephen Walther's post on ASP.NET MVC Tip #11 – Use Standard Controller Action Names would probably clarify you regarding to naming convention of MVC Action naming convention...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top