Naming conventions in ASP.NET MVC in ASP.NET MVC
What are the naming conventions to follow in ASP.NET MVC?
Mainly three naming convention should be followed in ASP.NET MVC application
- Controller - Its name must end with “controller” word. Eg. PersonalDetailsController, EmployeesController. Generally all controllers should be kept inside ~/Controllers folder of the project.
- Model - Model name (Singular name) corresponding to the database table should be similar to the database table name (not mandatory, however its ideal). For example, if the database table name is "PersonalDetails" (Plural), the model name should be "PersonalDetail" (Singular).
Generally, all models are kept inside the ~/Models folder of the project.
- ViewModel - View model is a class that contains properties from more than one Models, generally used to list data from more than one database tables, read more here. Ideally the name of the ViewModel should end with "ViewModel" word, for example PersonalDetailFileViewModel.
In general, the ViewModel name should contain the name of all Models whose properties are kept in this ViewModel (not mandatory, however I follow this, it helps us to know that what models constitutes this ViewModel). For example, if the ViewModel contains the properties of PersonalDetail and File models, I would give its name as PersonalDetailFileViewModel.
Sometimes ViewModel names are also kept based on the Action method in which it has to be used. For example, if we have to create a Login page where any of our Model would not fit so we can create a model named LoginViewModel that will only have UserName, Password, IsRememberMe properties that are needed for this page.
Views: 56695 | Post Order: 3
- Views – There should be a view folder inside ~/Views folder corresponding to each controller. Like for PersonalDetailsController, there should be a folder ~/Views/PersonalDetails, similarly for EmployeesController, there should be ~/Views/Employees folder.
- Areas - Areas are used to create different modules inside an ASP.NET MVC application. Each module can have their own set of controllers, models and views and the naming conventions of these will follows the same principal as described above.