dotnet publishdotnet publish

本文适用于:✓ .NET Core 1.x SDK ✓ .NET Core 2.x SDK This article applies to: ✓ .NET Core 1.x SDK .NET Core 2.x SDK


dotnet publish - 将应用程序及其依赖项打包到文件夹以部署到托管系统。dotnet publish - Packs the application and its dependencies into a folder for deployment to a hosting system.


dotnet publish [<PROJECT>] [-c|--configuration] [-f|--framework] [--force] [--manifest] [--no-build] [--no-dependencies]
    [--no-restore] [-o|--output] [-r|--runtime] [--self-contained] [-v|--verbosity] [--version-suffix]
dotnet publish [-h|--help]


dotnet publish 编译应用程序、读取 project 文件中指定的所有依赖项并将生成的文件集发布到目录。dotnet publish compiles the application, reads through its dependencies specified in the project file, and publishes the resulting set of files to a directory. 输出包括以下资产:The output includes the following assets:

  • 扩展名为 dll 的程序集中的中间语言 (IL) 代码。Intermediate Language (IL) code in an assembly with a dll extension.
  • 包含项目所有依赖项的 .deps.json 文件 。.deps.json file that includes all of the dependencies of the project.
  • .runtimeconfig.json 文件,其中指定了应用程序所需的共享运行时,以及运行时的其他配置选项(例如,垃圾回收类型)。.runtimeconfig.json file that specifies the shared runtime that the application expects, as well as other configuration options for the runtime (for example, garbage collection type).
  • 应用程序的依赖项,将这些依赖项从 NuGet 缓存复制到输出文件夹。The application's dependencies, which are copied from the NuGet cache into the output folder.

dotnet publish 命令的输出可供部署至托管系统(例如服务器、电脑、Mac、笔记本电脑)以便执行。The dotnet publish command's output is ready for deployment to a hosting system (for example, a server, PC, Mac, laptop) for execution. 若要准备用于部署的应用程序,这是唯一正式受支持的方法。It's the only officially supported way to prepare the application for deployment. 根据项目指定的部署的类型,托管系统不一定已在其上安装 .NET Core 共享运行时。Depending on the type of deployment that the project specifies, the hosting system may or may not have the .NET Core shared runtime installed on it. 有关详细信息,请参阅 .NET Core 应用程序部署For more information, see .NET Core Application Deployment. 有关已发布应用程序的目录结构,请参阅目录结构For the directory structure of a published application, see Directory structure.


从 .NET Core 2.0 开始,无需运行 dotnet restore,因为它由所有需要还原的命令隐式运行,如 dotnet builddotnet runStarting with .NET Core 2.0, you don't have to run dotnet restore because it's run implicitly by all commands that require a restore to occur, such as dotnet build and dotnet run. 在执行显式还原有意义的某些情况下,例如 Azure DevOps Services 中的持续集成生成中,或在需要显式控制还原发生时间的生成系统中,它仍然是有效的命令。It's still a valid command in certain scenarios where doing an explicit restore makes sense, such as continuous integration builds in Azure DevOps Services or in build systems that need to explicitly control the time at which the restore occurs.

以长格式传递命令时,该命令也支持 dotnet restore 选项(例如,--source)。This command also supports the dotnet restore options when passed in the long form (for example, --source). 不支持缩写选项,例如 -sShort form options, such as -s, are not supported.



要发布的项目。The project to publish. 它是 C#、F# 或 Visual Basic 项目文件的路径和文件名,或包含 C#、F# 或 Visual Basic 项目文件的目录的路径。It's either the path and filename of a C#, F#, or Visual Basic project file, or the path to a directory that contains a C#, F#, or Visual Basic project file. 如果未指定,则默认为当前目录。If not specified, it defaults to the current directory.


-c|--configuration {Debug|Release}

定义生成配置。Defines the build configuration. 默认值为 DebugThe default value is Debug.

-f|--framework <FRAMEWORK>

为指定的目标框架发布应用程序。Publishes the application for the specified target framework. 必须在项目文件中指定目标框架。You must specify the target framework in the project file.


强制解析所有依赖项,即使上次还原已成功,也不例外。Forces all dependencies to be resolved even if the last restore was successful. 指定此标记等同于删除 project.assets.json 文件 。Specifying this flag is the same as deleting the project.assets.json file.


打印出有关命令的简短帮助。Prints out a short help for the command.


指定一个或多个目标清单,用于剪裁与应用程序一同发布的一组包。Specifies one or several target manifests to use to trim the set of packages published with the app. 清单文件是 dotnet store 命令输出的一部分。The manifest file is part of the output of the dotnet store command. 若要指定多个清单,请为每个清单添加一个 --manifest 选项。To specify multiple manifests, add a --manifest option for each manifest. 自 .NET Core 2.0 SDK 起,可以使用此选项。This option is available starting with .NET Core 2.0 SDK.


发布前不生成项目。Doesn't build the project before publishing. 还将隐式设置 --no-restore 标记。It also implicitly sets the --no-restore flag.


忽略项目间引用,仅还原根项目。Ignores project-to-project references and only restores the root project.


运行此命令时不执行隐式还原。Doesn't execute an implicit restore when running the command.

-o|--output <OUTPUT_DIRECTORY>

指定输出目录的路径。Specifies the path for the output directory. 如未指定,对于依赖于框架的部署,将默认为 ./bin/[configuration]/[framework]/publish/ 或对于独立部署,将默认为 ./bin/[configuration]/[framework]/[runtime]/publish/ 。If not specified, it defaults to ./bin/[configuration]/[framework]/publish/ for a framework-dependent deployment or ./bin/[configuration]/[framework]/[runtime]/publish/ for a self-contained deployment. 如果路径是相对的,则生成的输出目录与项目文件位置相关,而与当前工作目录不相关。If the path is relative, the output directory generated is relative to the project file location, not to the current working directory.


与应用程序一同发布 .NET Core 运行时,因此无需在目标计算机上安装运行时。Publishes the .NET Core runtime with your application so the runtime doesn't need to be installed on the target machine. 如果指定了运行时标识符,默认值为 trueIf a runtime identifier is specified, its default value is true. 若要详细了解不同的部署类型,请参阅 .NET Core 应用程序部署For more information about the different deployment types, see .NET Core application deployment.


发布针对给定运行时的应用程序。Publishes the application for a given runtime. 这将在创建独立部署 (SCD) 时使用。This is used when creating a self-contained deployment (SCD). 有关运行时标识符 (RID) 的列表,请参阅 RID 目录For a list of Runtime Identifiers (RIDs), see the RID catalog. 默认为发布依赖于框架的部署 (FDD)Default is to publish a framework-dependent deployment (FDD).

-v|--verbosity <LEVEL>

设置命令的详细级别。Sets the verbosity level of the command. 允许使用的值为 q[uiet]m[inimal]n[ormal]d[etailed]diag[nostic]Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic].

--version-suffix <VERSION_SUFFIX>

定义版本后缀来替换项目文件的版本字段中的星号 (*)。Defines the version suffix to replace the asterisk (*) in the version field of the project file.


在当前目录中发布项目:Publish the project in the current directory:

dotnet publish

使用指定的项目文件发布应用程序:Publish the application using the specified project file:

dotnet publish ~/projects/app1/app1.csproj

使用 netcoreapp1.1 框架发布当前目录中的项目:Publish the project in the current directory using the netcoreapp1.1 framework:

dotnet publish --framework netcoreapp1.1

使用 netcoreapp1.1 框架和 OS X 10.10 的运行时发布当前应用程序(必须在项目文件中列出此 RID)。Publish the current application using the netcoreapp1.1 framework and the runtime for OS X 10.10 (you must list this RID in the project file).

dotnet publish --framework netcoreapp1.1 --runtime osx.10.11-x64

发布当前应用程序,但在还原操作期间不还原项目到项目 (P2P) 引用,只还原根项目(.NET Core SDK 2.0 及更高版本):Publish the current application but don't restore project-to-project (P2P) references, just the root project during the restore operation (.NET Core SDK 2.0 and later versions):

dotnet publish --no-dependencies

请参阅See also