Eric Anastas
Eric Anastas

Reputation: 22233

When should you create separate controllers with ASP.NET MVC?

I'm starting to learn ASP.NET MVC. I understand the concept of controllers, models, and views. Yet now that I'm starting to design my first site I'm a little lost as to what controllers I should be creating. Do most model objects have a corresponding controller? Or are there other considerations I should be making when grouping action methods into seperate controllers?

Upvotes: 3

Views: 624

Answers (2)

Michael Stum
Michael Stum

Reputation: 181094

Controllers logically separate small areas of functionality (Not to be confused with Areas in MVC which separate larger functional sections).

Do you have User Account Management for stuff like CreateAccount, ChangePassword? That's a UserAccountController.

Do you have functionality that allows people to create, view, delete Forum Postings? That's your ForumController.

Do you have functionality that allows people to manage their Preferences? That's your PreferencesController.

It's not so much 1 Controller per Model, it's 1 Controller per logical section in your app (which often indeed is one Model class). Some non-trivial MVC Sites work fine with only one controller, while my last project had eight of them.

Upvotes: 3

Paige Cook
Paige Cook

Reputation: 22555

In my limited experience with MVC so far, most of my controllers correspond to the model objects. I would also feel that you would create controllers for specific functionality within your site, like uploading files, etc.

Upvotes: 0

Related Questions