views:

142

answers:

3

How should one split up the controllers in an ASP.NET MVC site? For example, the default project has a HomeController and an AccountController. Should there be one controller for each section of the site, or something else?

I'm learning how to use the MVC framework, and any help would be appreciated.

A: 

The conventional way of doing it is to split up the controllers the same way as you split your views. Example:

/Views/Home has Index, register and contact views then you'd have a HomeController

/Views/Product has Show and List views then you'd have a ProductController

The simplest way is 1 controller for each folder in the Views folder.

Darko Z
A: 

There is no specific rule as such, as to when to make a new controller. You can make a new controller if you feel its gonna control a different section of the website.

Basically by having many controllers, one can logically divide the website into different sections.

San
+1  A: 

I think it depends on how large your site is going to be also. Having logical names for your controllers is the most important part, making your source easy to navigate is very important.

Generally I believe it's a good idea to take a leaf out of the CRUD book if you're not sure, especially if you're dealing with a data model that supports it. For a particular module (say Products) you would have a controller responsible for Create, Read, Update and Delete (as well as Index for viewing).

If you're site is less CRUDDY (like Stack Overflow probably) then splitting the controllers up into logical areas (Like perhaps "PostController", "SearchController" and so forth) might be of more use, but it really does depends on your site and it's architecture.

Sorry I can't be more helpful, in most cases it is best to learn by doing anyway. If you're new take the approach you think is best based on the suggestions and examples you've seen. One good think about .NET and ASP.NET MVC is that refactoring later is quite easy.

Odd