cli (command line interface)
.net core cli(命令行界面)是一个新的跨平台工具,用于创建,还原程序包,构建,运行和发布asp.net core应用程序。适用于任何类型的web应用程序的.net core cli命令使用进程外托管,即它使用kestrel服务器运行该应用程序。
到目前为止,我们创建的所有应用程序都使用visual studio。 visual studio在内部使用此.net cli命令还原,生成和发布应用程序。其他高级ide,编辑器和工具,例如visual studio code使用这些cli命令来支持创建,还原,发布和运行.net core应用程序。
当我们安装.net core sdk时,默认情况下还将安装.net core cli。因此,我们不需要在开发环境(即我们的本地计算机)上单独安装它。我们可以使用命令提示符来验证相同的内容,即是否已安装.net cli。要进行验证,请打开命令提示符(windows),终端(linux)并键入“ dotnet”,然后按enter,如下所示。如果它显示用法和帮助选项(如下图所示),则表明.net core cli已正确安装。
.net core cli命令结构
.net core cli命令结构不过是我们编写.net core cli命令的方式。 以下是.net core cli命令的命令结构:
dotnet <命令> <参数> <选项>
注意:所有.net core cli命令均以名为dotnet的驱动程序开头。 驱动程序即dotnet开始执行指定的命令。 在dotnet之后,我们需要指定命令(也称为动词)以执行特定操作。 每个命令后都可以带有参数和选项。
如何获取所有.net core命令
打开命令提示符(cmd),然后键入dotnet help,然后按enter,它将显示所有.net core cli命令。 下面给出了一些命令及其用法。
- add: add a package or reference to a .net project(添加一个包或对.net项目的引用).
- build: build a .net project(构建一个.net项目).
- build-server: interact with servers started by a build(与由构建启动的服务器进行交互).
- clean: clean build outputs of a .net project(清理.net项目的生成输出).
- help: show command-line help(显示命令行帮助).
- list: list project references for a .net project(列出.net项目的项目引用).
- msbuild: run microsoft build engine (msbuild) commands(运行microsoft build engine(msbuild)命令).
- new: create a new .net project or file(创建一个新的.net项目或文件).
- nuget: provides additional nuget commands(提供其他nuget命令).
- pack: create a nuget package(创建一个nuget包).
- publish: publish a .net project for deployment(发布一个.net项目以进行部署).
- remove: remove a package or reference from a .net project(从.net项目中删除程序包或引用).
- restore: restore dependencies specified in a .net project(恢复.net项目中指定的依赖项).
- run: build and run a .net project output(构建并运行.net项目输出).
- sln: modify visual studio solution files(修改visual studio解决方案文件).
- store: store the specified assemblies in the runtime package store(将指定的程序集存储在运行时程序包存储中).
- test: run unit tests using the test runner specified in a .net project(使用.net项目中指定的测试运行器运行单元测试).
- tool: install or manage tools that extend the .net experience(安装或管理可扩展.net体验的工具).
- vstest: run microsoft test engine (vstest) commands(运行microsoft测试引擎(vstest)命令)
项目修改命令
- add package: adds a package reference to a project.(将软件包引用添加到项目。)
- add reference: adds project-to-project (p2p) references.(添加项目间参考(p2p)。)
- remove package: removes package reference from the project.(从项目中删除软件包引用。)
- remove reference: removes project reference.(删除项目参考.)
- list reference: lists all project-to-project references.(列出所有项目间参考。)
高级命令
- nuget delete: deletes or un-lists a package from the server.(从服务器删除或取消列出软件包。)
- nuget locals: clear or lists nuget resources.(清除或列出nuget资源。)
- nuget push: pushes a package to the server and publishes it.(将软件包推送到服务器并发布。)
- msbuild: builds a project and all of its dependencies.(构建项目及其所有依赖项。)
- dotnet install script: script used to install .net core cli tools and the shared runtime.
(用于安装.net core cli工具和共享运行时的脚本)
(用于安装.net core cli工具和共享运行时的脚本)
使用.net core cli命令创建一个新项目
让我们在不使用visual studio的情况下,使用命令行界面创建,还原,构建和运行.net core控制台应用程序。 要创建一个新的.net core项目,我们必须使用“ new”命令以及模板名称参数。 我们可以使用cli创建控制台,类库,web,webapp,mvc,webapi,react,angular,react等项目。
以下命令使用template创建一个新的dotnet核心项目:
dotnet new <模板>
您可以使用以下方法找到模板列表:
dotnet new-l
键入dotnet new -l并按enter后,它将显示基于机器上安装的.net core版本的可用模板列表,如下图所示:
示例:使用.net core cli创建控制台应用程序
以下命令在当前目录中创建一个与当前目录同名的新控制台项目。
c:\users\zhouh\desktop\myapp>dotnet new console
一旦执行了以上命令,它将创建一个控制台应用程序,并且您将获得以下输出。
使用 .net cli 命令运行项目
要运行 .net core 项目,我们需要使用"dotnet run"命令,如下所示:在这里,你可以看到它显示输出 hello world!
使用 .net 核心 cli 命令生成项目
为了构建一个新的或现有的项目,我们需要使用以下“ dotnet build”命令来构建您的.net core项目:
到此这篇关于asp.net core命令行界面cli用法的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持代码网。
发表评论