Naming conventions - one rule for controllers, no rules for models and views

In ASP.NET, MVC controllers exist in the Controllers folder. Their names must end with Controller , otherwise things just don't work (you get an HTTP 404 error).

However, model names do not need to end with Model , and View names should not end with View .

This seems inconsistent ... why (in terms of MVC or design) do the controller names end with Controller ?

Do other MVC frameworks have this requirement?

Edit

Since this seems to be an agreement, I am not opposed to it (see the Customization Convention !), But I want to understand the reasons for this.

+6
model-view-controller asp.net-mvc naming-conventions
source share
2 answers

The controller agreement in this way makes it easy to find the controller without additional configuration. Adding a final destination controller makes it less likely that you accidentally discover an object through MVC routing.

There is also a built-in convention for Views. By default, the views should be in the folder with a name for your controller and have the same name as the action that calls them, this is what allows the View () method in your action to work without specifying the view. In any case, I often find that I define the view, but if you are looking for an agreement, this is definitely the one who is encouraged by the infrastructure.

From the point of view of the model you are right, there is no standard agreement. The reason for this is because the ASP.NET MVC framework never directly relates to models. For the controllers, an agreement is required to find them from the routing, and it needs an agreement for browsing to find them from the controllers ... but the models are available only from the controller logic, so the infrastructure does not need to know about them,

Speaking of this, I saw how most people build their models the same way they built their entities or domain model before MVC. If you use an active recording template, then name the models to correspond to the tables to which they are attached, if you are more focused on the domain, and then name the models corresponding to the part of the domain that they model. In addition, I saw how more and more people create a set of presentation models that are simply used to represent data in the user interface, and are created by pulling parts from different models of your domain. Models are by far the least self-confident part of ASP.NET MVC, but that's good, because people have very different ways of working in this area.

+8
source share

This is just an agreement not necessary! You can change this behavior by setting DefaultControllerFactory or by creating your own factory controller.

see here for more information. There are also several examples in the MvcContrib project that inject a controller from a dependency injection engine. check here .

+2
source share

All Articles