Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An Ideal Folder Structure for .NET MVC [closed]

When I started in .NET Webforms I didn't have much trouble finding a folder structure to follow since VS offered you application folders like "App_Code" and most app examples put "BLL", "DAL" inside there and so on.

But now in MVC, every example I check uses different structure, like no standards this time and I haven't found a good solution on Google or SO.

So, maybe we can share how we organize our MVC projects, may help others to make their own mind. Here is the structure for small to medium projects I use:

App_Data
Areas
    Admin
        Controllers
        Models
        Views
    MyAccount
        Controllers
        Models
        Views
Content
    Images
    Scripts
    Styles
Controllers
    HomeController.cs
Helpers
    ExtensionMethods    // I.e. based on HtmlHelper, use "helper" suffix
        MenuHelper.cs    // to be called as html.Menu()
    Utilities.cs    // Other generic (static) libraries, no suffix used
Models
    ViewModels    // for passing models to Views
        RegisterViewModel.cs    // use "ViewModel" suffix
    Customer.cs    // to extend models like adding Model Validation
Repositories
    CustomerRepository.cs    // use "Repository" suffix
Services
    CustomerService.cs    // use "Service" suffix, to move code away from controllers
Views
    Home
        Index.cshtml
        Register.cshtml
    Shared    // Site Layouts (Master templates), also put partials here
        SiteLayout.cshtml

What about yours?

like image 883
Nestor Avatar asked Dec 05 '10 08:12

Nestor


People also ask

What is MVC folder structure?

The Controllers folder contains class files for the controllers. A Controller handles users' request and returns a response. MVC requires the name of all controller files to end with "Controller". You will learn about the controller in the next section.

Which folder in an MVC application contains the style definition files?

This folder contains all the static files, such as css, images, icons, etc. The Site. css file inside this folder is the default styling that the application applies.

What is MVC Area folder?

Area allows us to partition the large application into smaller units where each unit contains a separate MVC folder structure, same as the default MVC folder structure. For example, a large enterprise application may have different modules like admin, finance, HR, marketing, etc.

Which MVC folder of ASP.NET is used for storing application data?

App_Data contains application data files including . mdf database files, XML files, and other data store files. The App_Data folder is used by ASP.NET to store an application's local database, such as the database for maintaining membership and role information.


3 Answers

I have found it simplifies deployment to have the website project contain only content (no compiled code).

Something like:

Web.Site project

   Content       Images       Css    Scripts    Views    web.config 

And move all compiled code into another project:

Web project

   Controllers    Filters    Models    ... 

Then, you can treat everything within the Web.Site project as needing to be deployed, and all required assemblies will be in Web.Site\bin.

Whether you are doing simple xcopy deployment, or using WiX to build an MSI package, this will make life a little easier.

like image 171
MJ Richardson Avatar answered Sep 28 '22 05:09

MJ Richardson


I second the two project approach. Jimmy Bogard has a nice post on the approach as well (make sure to go through all the comments).

I personally find that when I'm working on a part of an application I use related services, controllers, repositories, etc.. and when you put each of these files in a different folder it can get tedious going back and forth and finding them. After some playing around I've been following this format:

AppName.Web.UI

Scripts
Content
View

AppName.UI.Core

Attributes
Filters
Formatters
Helpers
Models
  Company
     Interfaces
       IController.cs
       IRepository.cs
       IService.cs
     ViewModels
       ViewModel1.cs
       ViewModel2.cs
     Controller.cs
     Repository.cs
     Service.cs
  User
    ....
Plugins (mailchimp, Twitter OAuth, etc..)
Global.asax (define all the code here rather than in the UI project)

Test Project

  ...

I think it depends how large your project is as to whether you further break down and use Interface and ViewModel sub folders. Its not perfect, but I've found it meshes better with the way I think.

The case can also be made to put your services and repositories into a third project (AppName.Core), leaving the AppName.Web.Core project encapsulating only Web relates parts (Attributes, Controllers. ViewModels, etc..). Again that really relates to the complexity of the project.

like image 30
TheRightChoyce Avatar answered Sep 28 '22 04:09

TheRightChoyce


So long as it's clear where things are, it doesn't matter much. I think it's just a matter of being consistent within your organization/group.

like image 45
dreadwail Avatar answered Sep 28 '22 05:09

dreadwail