Partitioning ASP.NET MVC Controllers

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

I am learning how to use the MVC framework and any help would be appreciated.

+4
source share
3 answers

I think it depends on how big your site will be. The logical names for your controllers are the most important part, which makes your source easy to navigate very important.

As a rule, I think that itโ€™s good to take a sheet from a CRUD book if you are not sure, especially if you are dealing with a data model that supports it. For a specific module (e.g. Products), you must have a controller responsible for creating, reading, updating, and deleting (as well as an index for viewing).

If you have less CRUDDY on the site (for example, Stack Overflow), then dividing the controllers into logical areas (for example, perhaps PostController, SearchController, etc.) may be more useful, but it really depends on your site and its architecture.

Sorry, I couldnโ€™t be more useful, in most cases itโ€™s best to learn by doing anyway. If you're a beginner, then an approach that you think is best based on the suggestions and examples you've seen. One good approach to .NET and ASP.NET MVC is that refactoring is later quite simple.

+1
source

The traditional way to do this is to separate controllers in the same way that you share your views. Example:

/ Views / Home has an index, a register and contacts, then you will have a HomeController

/ Views / Product has Show and List views, then you will have ProductController

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

0
source

There is no specific rule as such when you need to create a new controller. You can create a new controller if you feel that it will control another section of the website.

Basically, with many controllers, you can logically divide a website into different sections.

0
source

All Articles