ASP.NET Core 目录结构ASP.NET Core directory structure

作者:Luke LathamBy Luke Latham

发布目录包含应用的可部署资产,由 dotnet publish 命令生成。The publish directory contains the app's deployable assets produced by the dotnet publish command. 该目录包含:The directory contains:

  • 应用程序文件Application files
  • 配置文件Configuration files
  • 静态资产Static assets
  • packagePackages
  • 运行时(仅限独立部署A runtime (self-contained deployment only)
应用类型App Type 目录结构Directory Structure
依赖框架的部署Framework-dependent Deployment
  • publish†publish†
    • Views†(MVC 应用;如果未预编译视图)Views† (MVC apps; if views aren't precompiled)
    • Pages†(MVC 或 Razor 页应用;如果未预编译页)Pages† (MVC or Razor Pages apps; if pages aren't precompiled)
    • wwwroot†wwwroot†
    • *.dll 文件*.dll files
    • {ASSEMBLY NAME}.deps.json{ASSEMBLY NAME}.deps.json
    • {ASSEMBLY NAME}.dll{ASSEMBLY NAME}.dll
    • {ASSEMBLY NAME}.pdb{ASSEMBLY NAME}.pdb
    • {ASSEMBLY NAME}.Views.dll{ASSEMBLY NAME}.Views.dll
    • {ASSEMBLY NAME}.Views.pdb{ASSEMBLY NAME}.Views.pdb
    • {ASSEMBLY NAME}.runtimeconfig.json{ASSEMBLY NAME}.runtimeconfig.json
    • web.config(IIS 部署)web.config (IIS deployments)
独立部署Self-contained Deployment
  • publish†publish†
    • Views†(MVC 应用;如果未预编译视图)Views† (MVC apps; if views aren't precompiled)
    • Pages†(MVC 或 Razor 页应用;如果未预编译页)Pages† (MVC or Razor Pages apps; if pages aren't precompiled)
    • wwwroot†wwwroot†
    • *.dll 文件*.dll files
    • {ASSEMBLY NAME}.deps.json{ASSEMBLY NAME}.deps.json
    • {ASSEMBLY NAME}.dll{ASSEMBLY NAME}.dll
    • {ASSEMBLY NAME}.exe{ASSEMBLY NAME}.exe
    • {ASSEMBLY NAME}.pdb{ASSEMBLY NAME}.pdb
    • {ASSEMBLY NAME}.Views.dll{ASSEMBLY NAME}.Views.dll
    • {ASSEMBLY NAME}.Views.pdb{ASSEMBLY NAME}.Views.pdb
    • {ASSEMBLY NAME}.runtimeconfig.json{ASSEMBLY NAME}.runtimeconfig.json
    • web.config(IIS 部署)web.config (IIS deployments)

†指示目录†Indicates a directory

publish 目录代表部署的内容根路径,也称为应用程序基路径。The publish directory represents the content root path, also called the application base path, of the deployment. 无论对服务器上已部署应用的 publish 目录如何命名,其位置都可作为托管应用的服务器物理路径。Whatever name is given to the publish directory of the deployed app on the server, its location serves as the server's physical path to the hosted app.

wwwroot 目录(如果存在)仅包含静态资产。The wwwroot directory, if present, only contains static assets.

创建 Logs 文件夹对于 ASP.NET 核心模块增强的调试日志记录来说非常有用。Creating a Logs folder is useful for ASP.NET Core Module enhanced debug logging. 提供给 <handlerSetting> 值的路径中的文件夹不是由模块自动创建的,并且应该预先存在于部署中以允许模块编写调试日志。Folders in the path provided to the <handlerSetting> value aren't created by the module automatically and should pre-exist in the deployment to allow the module to write the debug log.

可以使用以下两种方法之一为部署创建 Logs 目录:A Logs directory can be created for the deployment using one of the following two approaches:

  • 向项目添加以下 <Target> 元素:Add the following <Target> element to the project file:

    <Target Name="CreateLogsFolder" AfterTargets="Publish">
       <MakeDir Directories="$(PublishDir)Logs" 
                Condition="!Exists('$(PublishDir)Logs')" />
       <WriteLinesToFile File="$(PublishDir)Logs\.log" 
                         Lines="Generated file" 
                         Overwrite="True" 
                         Condition="!Exists('$(PublishDir)Logs\.log')" />
    </Target>
    

    <MakeDir> 元素在发布的输出中创建一个空的 Logs 文件夹。The <MakeDir> element creates an empty Logs folder in the published output. 该元素使用 PublishDir 属性来确定创建文件夹的目标位置。The element uses the PublishDir property to determine the target location for creating the folder. 几种部署方法(如 Web 部署)均在部署期间跳过空文件夹。Several deployment methods, such as Web Deploy, skip empty folders during deployment. <WriteLinesToFile> 元素在 Logs 文件夹中生成一个文件,该文件可确保将文件夹部署到服务器。The <WriteLinesToFile> element generates a file in the Logs folder, which guarantees deployment of the folder to the server. 如果工作进程不具有对目标文件夹的写入权限,则使用此方法的文件夹创建操作将失败。Folder creation using this approach fails if the worker process doesn't have write access to the target folder.

  • 在部署中的服务器上物理创建 Logs 目录。Physically create the Logs directory on the server in the deployment.

部署目录需要读取/执行权限。The deployment directory requires Read/Execute permissions. Logs 目录需要读/写权限。The Logs directory requires Read/Write permissions. 将文件写入其他目录需要读/写权限。Additional directories where files are written require Read/Write permissions.

其他资源Additional resources