7

In ASP.NET MVC, we're required to use the suffix "Controller" for all controllers. This seems unnecessarily restrictive - is there a technical reason for it?

I'm mostly just curious, but can see situations where more flexible naming rules could improve code organization. Couldn't the discovery of possible controller classes be easily made using reflection to search for Controller derived classes? Or require that controller classes be marked with a ControllerAttribute?

0

2 Answers 2

14

The MVC community is heavily influenced by Ruby on Rails, which values "convention over configuration". By just naming things consistently, the application can run with zero configuration.

Sign up to request clarification or add additional context in comments.

1 Comment

You beat me to it! Here's a link: en.wikipedia.org/wiki/Convention_over_configuration
4

One of the benefits of this convention is that it's common to have a URL segment, controller, and a model class all have the same name.

URL: /product/ Controller: Product : Controller Model: Product

This would cause a naming conflict. So we made a convention to have controller names suffixed with "Controller" to avoid this conflict. However, you can override this behavior via our extensibility APIs.

1 Comment

Actually Phil... By REST those URL segments should name collections which means that controllers are usually plural as in ProductsController (/products/1) while model entity is singular as Product because collection displaying views will use model IEnumerable<Product>. So there would be no clashes anyway. But convention is good, because it's used elswhere as well (without people complaining) as in attributes, collections, dictionaries, lists etc. These types also conventionally use suffixes. For development usability reasons of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.