locked
MVC Folder organization RRS feed

  • Question

  • User-29703693 posted

    I'm working on my first MVC project with the following sections: Home, Student, University Liaison, Admin, Site Admin.  All of the sections will be pretty small, except for the student section which will have about 4 subsections (folders).  How should I organize the folders and set up the routing for such a project?

    I was thinking about having a separate Student folder with a Controllers, ViewModels and Views folder in their.  But I'm not sure if that is the best practice, having two separate places for controllers, viewmodels and views. 

    Saturday, October 19, 2019 9:01 PM

Answers

  • User1120430333 posted

    I was thinking about having a separate Student folder with a Controllers, ViewModels and Views folder in their. But I'm not sure if that is the best practice, having two separate places for controllers, viewmodels and views.

    The controller belongs in the controller folder. A viewmodel belongs in the Models folder and the views belong in the View folder. You should look into ASP.NET MVC Areas if you need further segregation.

    Also keep this in mind.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    https://www.c-sharpcorner.com/UploadFile/56fb14/understanding-separation-of-concern-and-Asp-Net-mvc/

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/overview/understanding-models-views-and-controllers-cs

    <copied>

    An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic. For example, if you are using the Microsoft Entity Framework to access your database, then you would create your Entity Framework classes (your .edmx file) in the Models folder.

    A view should contain only logic related to generating the user interface. A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action (flow control). Everything else should be contained in the model.

    In general, you should strive for fat models and skinny controllers. Your controller methods should contain only a few lines of code. If a controller action gets too fat, then you should consider moving the logic out to a new class in the Models folder.

    <end>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 20, 2019 12:41 PM