ASP. net mvc Tip #11-use a standard controller operation name

Source: Internet
Author: User
Tags constant definition

Address: http://weblogs.asp.net/stephenwalther/archive/2008/06/27/asp-net-mvc-tip-11-use-standard-controller-action-names.aspx

 

Abstract: In this Tip, Stephen Walther recommends that you use a standard name for the Controller operation to make your code clearer to other developers.

 

Naming Conventions can make it easier for other developers to read your code-And yourself-in the future. Naming Conventions can also save you time and avoid the effort to discuss the "correct" naming method. In this Tip, we recommend that you use a standard name for ASP. net mvc controller operations.

 

The following table lists the standard names of controller operations:

 

Operation Example URL Description
Details /Product/Details/5 Displays a single resource, such as a database record. For example, a separate Product with Id 5 is displayed.
Index /Product/Index Displays a group of resources. For example, all products in the product table in the database are displayed.
Create /Product/Create Displays the form used to create a new resource. For example, a form is displayed for creating a new product.
Insert /Product/Insert Insert a new resource into the database. Generally, you should redirect to other operations after one insert.
Edit /Product/Edit/5 Displays the form used to edit an existing resource. For example, a form is displayed for editing a product with the Id of 5.
Update /Product/Update/5 Update existing resources in the database. Generally, you should redirect to other operations after an update is completed.
Destroy /Product/Destroy/5 A page is displayed to check whether you really want to delete a resource from the database.
Delete /Product/Delete/5 Deletes a resource from the database. Generally, you should redirect to other operations after a deletion is completed.
Login /Home/Login A logon form is displayed.
Logout /Home/Logout Log out of a user. Generally, you should redirect to other operations after cancellation.
Authenticate /Home/Authenticate Verify a pair of user names and passwords. Generally, after a verification is completed, you should redirect to other operations.



My operation names (very rough) are based on the naming conventions Adam Tybor uses in its MvcContrib project's Simply Restful Route Handler. The project is located:

 

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

 

However, Adam Tybor's Conventions are significantly different from what I suggest here. Next I will introduce the differences between the two.

 

First, Adam Tybor uses different HTTP predicates to indicate the Controller operation to be executed (he has a "Restful", he is being a REST purist ). For example, Adam Tybor recommends that the same URL be used to delete or update a resource:

 

/Product/34

 

When an http put request is sent to this URL, an existing record is updated. An existing record is deleted when an http delete request uses the same URL. The reason Adam Tybor proposed is meaningful. His suggestion is consistent with the purpose of these HTTP predicates. However, it is difficult for Adam Tybor to work with the default ASP. net mvc route. Therefore, we recommend that you use a clear URL for deletion and update:

 

/Product/Update/34

/Product/Delete/34

 

If you use these naming conventions, you can use the default ASP. net mvc route table.

 

Another important difference between the Controller operation naming conventions I suggest and other naming conventions I have seen is the name of an operation for creating a new resource. I have seen someone using the following operation name when creating a new resource:

 

/Product/New

/Product/Create

 

Generally, the New operation is used to display the form used to Create a New resource, while the Create operation actually performs the database insert operation.

 

The problem is that using New as the operation name may conflict with the C # and Visual Basic. NET keywords. When creating a New controller operation in a Visual Basic. NET application, you must always remember to escape its name to [New]. The constant definition of a name will soon become boring and confusing for those who do not understand the meaning of the square brackets. Therefore, we recommend that you use the Create and Insert pairs instead of the New and Create pairs.

 

You may have noticed that I am trying to match the SQL statement name in the (actual) executed database operation. Therefore, the following URL should be used to display the form:

 

/Product/Create

/Product/Edit/1

/Product/Destroy/1

 

The following URL is used to perform database operations:

 

/Product/Insert

/Product/Update/1

/Product/Delete/1

 

It is always risky to make suggestions on naming conventions, because developers always have strong personal opinions on how to name the conventions correctly. However, I hope this Tip can serve as the starting point for developing a naming convention.

 

------

 

This article has no code. The ad is also a. NET regular expression library, http://regex-lib.net.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.