I’m starting with MVC and have a newbie question. What would be the logic criteria to define what a controller should encompass? For example, say a website has a ‘help’ section. In there, there are several options like: ‘about us’, ‘return instructions’, ‘contact us’, ’employment opportunities’. Each would then be accessed like ‘mysite.com/help/aboutus’, ‘mysite.com/help/returns’, ‘mysite.com/help/contactus’, etc.
My question is, should I have a ‘help’ controller that has ‘about us’, ‘returns’, ‘contact us’, ’employment’ as actions with their respective view, or should each of those be a different controller-action-view set? What should be the line of reasoning to determine when to separate controllers?
2
That is entirely up to you.
With ASP.Net MVC3 you can choose if you want the default routing to do the work, or if you want to manually create some sexy URLs. You can create routes that look to be within the same controller but they can in fact be in many different controllers.
The criteria you should use when deciding the organization of Controllers and their method is, are these actions related in some way?
Following your example for Help. It would make sense to keep them within the same controller because they do offer “Help” in one way or another.
Typically a controller maps a class to a base URL, usually a webpage e.g. mysite.com/help, so you have the right idea. The class methods are typically handlers for actions triggered by controls on the page – which typically have an appended path e.g. mysite.com/help/contact – if say ‘contact’ were a button that opened the user’s default mail program w/ the company’s address populated in the ‘To’ field.