Subscribe to this Blog

Subscribe to this Blog

ASP.NET MVC Tip #11 – Use Standard Controller Action Names - Stephen Walther on ASP.NET MVC

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.

Published Friday, June 27, 2008 12:13 PM by swalther
Filed under: , ,

Comments

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

Friday, June 27, 2008 4:06 PM by Kyle West

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.

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

Sunday, June 29, 2008 8:17 AM by byron

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.

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

Sunday, June 29, 2008 11:50 AM by swalther

@Byron - Adam Tybor's SimplyRestfulRouteHandler provides a Ruby on Rails like routing experience for ASP.NET MVC. See: www.codeplex.com/.../View.aspx.

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).

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

Sunday, June 29, 2008 11:57 AM by swalther

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

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

Thursday, August 21, 2008 4:10 AM by AndrewPeters

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

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

Saturday, May 14, 2011 5:13 AM by weblogs.asp.net

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

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

Tuesday, June 28, 2011 12:48 PM by weblogs.asp.net

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