asp.net-mvc .NET MVC 的理想文件夹结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4357911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:37:10  来源:igfitidea点击:

An Ideal Folder Structure for .NET MVC

asp.net-mvcasp.net-mvc-2asp.net-mvc-3naming-conventionsconventions

提问by Nestor

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.

当我开始使用 .NET Webforms 时,我很容易找到要遵循的文件夹结构,因为 VS 为您提供了诸如“App_Code”之类的应用程序文件夹,并且大多数应用程序示例都在其中放置了“BLL”、“DAL”等等。

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.

但是现在在 MVC 中,我检查的每个示例都使用不同的结构,就像这次没有标准一样,我还没有在 Google 或 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:

所以,也许我们可以分享我们如何组织我们的 MVC 项目,可能会帮助其他人做出自己的决定。这是我使用的中小型项目的结构:

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?

你的呢?

回答by MJ Richardson

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.

然后,您可以将 Web.Site 项目中的所有内容都视为需要部署,并且所有必需的程序集都将位于 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.

无论您是进行简单的 xcopy 部署,还是使用 WiX 构建 MSI 包,这都会让生活变得更轻松。

回答by TheRightChoyce

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

我支持两个项目的方法。Jimmy Bogard 也有一篇关于该方法好文章(确保仔细阅读所有评论)。

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

应用名称.Web.UI

Scripts
Content
View

AppName.UI.Core

应用名称.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.

我认为这取决于您的项目有多大,是否进一步分解并使用 Interface 和 ViewModel 子文件夹。它并不完美,但我发现它更符合我的想法。

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.

也可以将您的服务和存储库放入第三个项目 (AppName.Core) 中,让 AppName.Web.Core 项目仅封装与 Web 相关的部分(属性、控制器、视图模型等)。同样,这确实与项目的复杂性有关。

回答by dreadwail

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.

只要清楚事情在哪里,就没有太大关系。我认为这只是在您的组织/小组内保持一致的问题。