ASP.NET MVC Tip #11 – Use Standard Controller Action Names

In this tip, I recommend that you use standard names for your controller actions in order to make your code more transparent to other developers.

Adopting naming conventions makes your code easier to read for other developers and your future self. Naming conventions also saves you time so that you can prevent endless debates about the “right” way to name something. In this tip, I recommend standard names for ASP.NET MVC controller actions.

Here is the table of suggested standard names for controller actions:

Action

Sample URL

Description

Details

/Product/Details/5

Displays a single resource such as a database record. For example, displays a single Product with an Id of 5.

Index

/Product/Index

Displays a collection of resources. For example, displays all of the products in the products database table.

Create

/Product/Create

Displays a form for creating a new resource. For example, displays a form for creating a new product.

Insert

/Product/Insert

Inserts a new resource into the database. Typically, you redirect to another action after performing an Insert.

Edit

/Product/Edit/5

Displays a form for editing an existing resource. For example, displays a form for editing a product with an Id of 5.

Update

/Product/Update/5

Updates existing resources in the database. Typically, you redirect to another action after performing an Update.

Destroy

/Product/Destroy/5

Displays a page that confirms whether or not you want to delete a resource from the database.

Delete

/Product/Delete/5

Deletes a resource from the database. Typically, you redirect to another action after performing a Delete.

Login

/Home/Login

Displays a login form.

Logout

/Home/Logout

Logs out a user. Typically, you redirect to another action after performing a Logout.

Authenticate

/Home/Authenticate

Authenticates a user name and password. Typically, you redirect to another action after performing an Authenticate.

I based these action names (very roughly) on Adam Tybor’s naming conventions for his Simply Restful Route Handler included in the MvcContrib project at:

http://www.codeplex.com/MVCContrib/Wiki/View.aspx?title=SimplyRestfulRouting&referringTitle=Documentation

However, there are significant differences between Adam Tybor’s conventions and the ones that I recommend here. Let me explain the changes.

First, Adam Tybor takes advantage of the different HTTP verbs to indicate the controller action to execute (he is being a REST purist). For example, Adam Tybor recommends that the same URL be used to either delete or update a resource:

/Product/34

When this URL is requested with an HTTP PUT then an existing record is updated. When the same URL is requested with an HTTP DELETE then an existing record is deleted. Adam Tybor’s reasoning here makes sense. His recommendation fits the intended purpose of these HTTP verbs. However, Adam Tybor’s recommendations do not work with the default ASP.NET MVC route. Therefore, I recommend that you use distinct URLs when performing deletes and updates:

/Product/Update/34

/Product/Delete/34

If you use these naming conventions, then you can use the default ASP.NET MVC route table.

One other important difference between the action naming convention that I recommend here and other naming conventions that I have seen concern the naming of the actions for creating a new resource. Typically, I see the following action names used when creating a new resource:

/Product/New

/Product/Create

Typically, the New action is used to display a form for creating a new resource and the Create action actually performs the database Insert operation.

The problem with using an action named New is that this action name conflicts with a C# and Visual Basic .NET keyword. When creating a controller action named New in a Visual Basic .NET application, you must always remember to escape the name like this [New]. Constantly escaping a name gets tiresome quickly and it is also confusing to people who don't understand the meaning of the square brackets. Therefore, I recommend the pair Create and Insert instead of the pair New and Create.

You might notice that I have tried to align actions that (typically) perform a database operation with their corresponding SQL statement name. Therefore, the following URLs are used to display forms:

/Product/Create

/Product/Edit/1

/Product/Destroy/1

And the following URLs are used to perform database operations:

/Product/Insert

/Product/Update/1

/Product/Delete/1

Proposing naming conventions is always risky because developers have such strong personal opinions on the right ways to name things. However, I hope this tip can act as a starting point for whatever naming conventions that you develop.

7 Comments

  • I agree with all but Show .. I prefer Detail. Show could be confused with showing any number of things .. i.e. a list of products.

  • guys, be careful not to re-invent the wheel here. rails already have adopted a convention in REST, lets try and leverage off what they are doing.

  • @Byron - Adam Tybor's SimplyRestfulRouteHandler provides a Ruby on Rails like routing experience for ASP.NET MVC. See: http://www.codeplex.com/MVCContrib/Wiki/View.aspx?title=SimplyRestfulRouting&referringTitle=Documentation.

    What I wanted to do here was to propose action naming conventions that would work with the default ASP.NET MVC route table. Also, I need a convention that works for both C# and VB.NET developers (so naming an action New won't work).

  • @Kyle - I replaced Show in the table with Details. I agree that Details is more accurate, descriptive, and intuitive than Show.

  • Login, Logout etc. are not RESTful - They can be made so if you think of the resource as a "session".

  • Asp net mvc tip 11 use standard controller action names.. Great! :)

  • Asp net mvc tip 11 use standard controller action names.. Great! :)

Comments have been disabled for this content.