dotnet 运行dotnet run

本主题适用于:✓ .NET Core 1.x SDK .NET Core 2.x SDKThis topic applies to: ✓ .NET Core 1.x SDK .NET Core 2.x SDK

nameName

dotnet run - 无需任何显式编译或启动命令即可运行源代码。dotnet run - Runs source code without any explicit compile or launch commands.

摘要Synopsis

dotnet run [-c|--configuration] [-f|--framework] [--force] [--launch-profile] [--no-build] [--no-dependencies]
    [--no-launch-profile] [--no-restore] [-p|--project] [--runtime] [-v|--verbosity] [[--] [application arguments]]
dotnet run [-h|--help]

说明Description

dotnet run 命令为从源代码使用一个命令运行应用程序提供了一个方便的选项。The dotnet run command provides a convenient option to run your application from the source code with one command. 这对从命令行中进行快速迭代开发很有帮助。It's useful for fast iterative development from the command line. 命令取决于生成代码的 dotnet build 命令。The command depends on the dotnet build command to build the code. 对于此生成的任何要求,例如项目必须首先还原,同样适用于 dotnet runAny requirements for the build, such as that the project must be restored first, apply to dotnet run as well.

输出文件会写入到默认位置,即 bin/<configuration>/<target>Output files are written into the default location, which is bin/<configuration>/<target>. 例如,如果具有 netcoreapp2.1 应用程序并且运行 dotnet run,则输出置于 bin/Debug/netcoreapp2.1For example if you have a netcoreapp2.1 application and you run dotnet run, the output is placed in bin/Debug/netcoreapp2.1. 将根据需要覆盖文件。Files are overwritten as needed. 临时文件将置于 obj 目录。Temporary files are placed in the obj directory.

如果该项目指定多个框架,在不使用 -f|--framework <FRAMEWORK> 选项指定框架时,执行 dotnet run 将导致错误。If the project specifies multiple frameworks, executing dotnet run results in an error unless the -f|--framework <FRAMEWORK> option is used to specify the framework.

在项目上下文,而不是生成程序集中使用 dotnet run 命令。The dotnet run command is used in the context of projects, not built assemblies. 如果尝试改为运行依赖于框架的应用程序 DLL,则必须在不使用命令的情况下使用 dotnetIf you're trying to run a framework-dependent application DLL instead, you must use dotnet without a command. 例如,若要运行 myapp.dll,请使用:For example, to run myapp.dll, use:

dotnet myapp.dll

有关 dotnet 驱动程序的详细信息,请参阅 .NET Core 命令行工具 (CLI) 主题。For more information on the dotnet driver, see the .NET Core Command Line Tools (CLI) topic.

若要运行应用程序,dotnet run 命令需从 NuGet 缓存解析共享运行时之外的应用程序依赖项。To run the application, the dotnet run command resolves the dependencies of the application that are outside of the shared runtime from the NuGet cache. 因为它使用缓存的依赖项,因此,不推荐在生产中使用 dotnet run 来运行应用程序。Because it uses cached dependencies, it's not recommended to use dotnet run to run applications in production. 相反,使用 dotnet publish 命令创建部署,并部署已发布的输出。Instead, create a deployment using the dotnet publish command and deploy the published output.

备注

从 .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, such as dotnet build and dotnet run, that require a restore to occur. 在执行显式还原有意义的某些情况下,例如 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.

选项Options

--

将参数分隔到正在运行的应用程序的参数的 dotnet runDelimits arguments to dotnet run from arguments for the application being run. 在此分隔符后的所有参数均传递给已运行的应用程序。All arguments after this delimiter are passed to the application run.

-c|--configuration {Debug|Release}

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

-f|--framework <FRAMEWORK>

使用指定框架生成并运行应用。Builds and runs the app using the specified framework. 框架必须在项目文件中进行指定。The framework must be specified in the project file.

--force

强制解析所有依赖项,即使上次还原已成功,也不例外。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.

-h|--help

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

--launch-profile <NAME>

启动应用程序时要使用的启动配置文件(若有)的名称。The name of the launch profile (if any) to use when launching the application. 启动配置文件在 launchSettings.json 文件中进行定义,通常称为 DevelopmentStagingProductionLaunch profiles are defined in the launchSettings.json file and are typically called Development, Staging, and Production. 有关详细信息,请参阅使用多个环境For more information, see Working with multiple environments.

--no-build

运行前不生成项目。Doesn't build the project before running. 还隐式设置 --no-restore 标记。It also implicit sets the --no-restore flag.

--no-dependencies

当使用项目到项目 (P2P) 引用还原项目时,还原根项目,不还原引用。When restoring a project with project-to-project (P2P) references, restores the root project and not the references.

--no-launch-profile

不尝试使用 launchSettings.json 配置应用程序。Doesn't try to use launchSettings.json to configure the application.

--no-restore

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

-p|--project <PATH>

指定要运行的项目文件的路径(文件夹名称或完整路径)。Specifies the path of the project file to run (folder name or full path). 如果未指定,则默认为当前目录。If not specified, it defaults to the current directory.

--runtime <RUNTIME_IDENTIFIER>

指定要为其还原包的目标运行时。Specifies the target runtime to restore packages for. 有关运行时标识符 (RID) 的列表,请参阅 RID 目录For a list of Runtime Identifiers (RIDs), see the RID catalog.

-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].

示例Examples

运行当前目录中的项目:Run the project in the current directory:

dotnet run

运行指定的项目:Run the specified project:

dotnet run --project ./projects/proj1/proj1.csproj

运行当前目录中的项目(在本例中,--help 参数被传递到应用程序,因为使用了空白的 -- 选项):Run the project in the current directory (the --help argument in this example is passed to the application, since the blank -- option is used):

dotnet run --configuration Release -- --help

在当前仅显示最小输出的目录中还原项目的依赖项和工具,然后运行项目(.NET Core SDK 2.0 及更高版本):Restore dependencies and tools for the project in the current directory only showing minimal output and then run the project: (.NET Core SDK 2.0 and later versions):

dotnet run --verbosity m