views:

296

answers:

4

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

However, Model names don't have to end Model and View names don't have to end with View.

This seems inconsistent...why (from an MVC or design standpoint) do controller names have to end Controller?

Do other MVC frameworks have this requirement?

Edit

Since this appears to be the convention I am not advocating going against it (see Convention over Configuration!), but I want to understand the reasons behind it.

+2  A: 

I was wondering the same thing.

I really dislike that controller classes should end with "-Controller". Adds unnecessary text. Better to specify that the directory should be called "Controllers" and what's inside are all controllers and nothing else.

Actually, even a better idea: allow to call classes the way we please and add an attribute to define a contoller:

[ControllerName="Home"]
public class HowAreYouToday : Controller
{
}

They already have an "ActionName" attribute, why not extend the concept?

User
+2  A: 

That is just a convention not a requirement! You can change this behavor by customizing the DefaultControllerFactory or creating your own controller factory.

see here for more info. Also there are some examples in MvcContrib project which inject the controller from a Dependency Injection engine. check it out here.

Hadi Eskandari
+3  A: 

The controller convention is so routing can easily find the controller without additional configuration. Adding the required Controller ending makes it less likely that you would accidentally expose an object through MVC routing.

There is a built in convention for Views as well. By default views should be in a folder named for your controller and be named the same as the action calling them, this is what enables the method call View() in your action to work without specifying the view. I often find myself specifying the view anyway, but if you are looking for a convention this is definitely the one encouraged by the framework.

From a model standpoint you are correct, there is no standard convention. The reason for this is because the ASP.NET MVC framework never directly touches the models. It needs a convention for the controllers to find them from routing, and it needs a convention for views to find them from the controllers... but models are only accessed from logic in the controller so the framework doesn't need to know about them.

That being said I have seen most people build their Models just like they built their entities or Domain model before MVC. If you are using an active record pattern then name the models to correspond with the tables they are mapped to, if you are focusing more on a domain then name the models to correspond with the part of the domain they are modeling. Also, I have seen more and more people creating a set of view models that are just used for presenting data to the UI and are created by pulling in parts from various models in your domain. Models are definitely the least opinionated part of ASP.NET MVC, but that is a good thing imo since people have very different ways they like to work in this area.

James Avery
A: 

I was thinking about the following:

CategoryController and CategoryAdminController

To enable to extend convention so you can call "Category" and (depending on some other route value) to decide which one to call.

Andrej Kaurin