一、前言
- 本篇开发环境?
1、操作系统: Windows 10 X64
2、SDK: .NET Core 2.0 Preview
二、安装 .NET Core SDK
1、下载 .NET Core
下载地址:https://www.microsoft.com/net/download/core
根据自己电脑情况选择对应版本即可
.NET CORE 2.0下载地址:https://aka.ms/dotnet-sdk-2.0.0-preview2-win-x642、安装
微软出品,一键安装,只需一步,看图:
三、熟悉命令(cmd)
- 1、查看版本
#使用命令提示符(cmd)或者Windows PowerShell
dotnet --version
- 2、可选参数介绍
参数 | 介绍(en) | 介绍(ken的翻译) |
---|---|---|
new | Initialize .NET projects. | 初始化项目(相当于通过VS模板新建项目) |
restore | Restore dependencies specified in the .NET project. | 还原项目中的依赖(相当于VS创建ASP.NET MVC,添加相关依赖) |
run | Compiles and immediately executes a .NET project. | 启动项目 |
build | Builds a .NET project. | 编译项目 |
publish | Publishes a .NET project for deployment (including the runtime). | 发布项目(包含runtime) |
test | Runs unit tests using the test runner specified in the project. | 启动单元测试 |
pack | Creates a NuGet package. | 创建nuget包 |
migrate | Migrates a project.json based project to a msbuild based project. | 迁移基于project.json,以兼容msbuild的编译 |
clean | Clean build output(s). | 清除项目中编译产生的输出 |
sln | Modify solution (SLN) files. | 修改解决方案文件.sln |
add | Add reference to the project. | 添加引用 |
remove | Remove reference from the project. | 移除引用 |
list | List reference in the project. | 列出项目中的引用 |
nuget | Provides additional NuGet commands. | 通过nuget参数并附加一些参数,可以进行nuget包管理的一些操作 |
msbuild | Runs Microsoft Build Engine (MSBuild). | 使用msbuild进行编译 |
vstest | Runs Microsoft Test Execution Command Line Tool. | 启动命令行测试工具 |
-v/—version | Display .NET Core SDK version. | 查看.NET Core SDK版本 |
-i/—info | Display .NET Core information. | 查看.NET Core 详细信息 |
-d/—diagnostics | Enable diagnostic output. | 启用诊断 |
-v/—verbosity | Set the verbosity level of the command. | 设置冗长命令集? |
-h/—help | Show help. | 查看帮助 |
四、HelloWorld项目
- 1、创建项目
#使用命令提示符(cmd)或者Windows PowerShell
#1、打开项目文件夹(如果没有就先创建好)
d:
cd d:\projects
#2、创建项目
dotnet new console -o helloworld
#dotnet new :创建&初始化项目
#console : 模板类型(相当于VS创建项目选择控制台应用程序)
#-o :指定output路径名,可以理解为项目文件夹名称,默认项目名称=项目文件夹名称,也可以用-n 单独指定项目名称
#dotnet new console -n helloworld 效果等同于 dotnet new console -o helloworld
#执行输出:
The template "Console Application" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on helloworld\helloworld.csproj...
Restoring packages for D:\Projects\helloworld\helloworld.csproj...
Installing Microsoft.NETCore.DotNetAppHost 2.0.0-preview2-25407-01.
Installing Microsoft.Packaging.Tools 1.0.0-preview2-25401-01.
Installing Microsoft.NETCore.DotNetHostResolver 2.0.0-preview2-25407-01.
Installing NETStandard.Library 2.0.0-preview2-25401-01.
Installing Microsoft.NETCore.Platforms 2.0.0-preview2-25405-01.
Installing Microsoft.NETCore.DotNetHostPolicy 2.0.0-preview2-25407-01.
Installing Microsoft.NETCore.App 2.0.0-preview2-25407-01.
Generating MSBuild file D:\Projects\helloworld\obj\helloworld.csproj.nuget.g.props.
Generating MSBuild file D:\Projects\helloworld\obj\helloworld.csproj.nuget.g.targets.
Restore completed in 7.04 sec for D:\Projects\helloworld\helloworld.csproj.
Restore succeeded.
#项目文件就在d:\projects\hellworld中
- 2、输出结果分析
#1、显示根据指定dotnet new console -o helloworld模板创建了项目
The template "Console Application" was created successfully.
#2、然后又主动调用了dotnet restore命令来还原项目的引用,主动安装依赖
Processing post-creation actions...
Running 'dotnet restore' on helloworld\helloworld.csproj...
Restoring packages for D:\Projects\helloworld\helloworld.csproj...
- 3、运行
cd d:\projects\helloworld
dotnet run
#运行结果
Hello World!
# 恭喜你,你的第一个.NET Core应用程序就这么诞生了
#源代码请查 d:\projects\hellworld\Program.cs 文件
五、备注
- 支持的项目模板
Templates | Short Name | Language | Tags |
---|---|---|---|
Console Application | console | [C#], F#, VB | Common/Console |
Class library | classlib | [C#], F#, VB | Common/Library |
Unit Test Project | mstest | [C#], F#, VB | Test/MSTest |
xUnit Test Project | xunit | [C#], F#, VB | Test/xUnit |
ASP.NET Core Empty | web | [C#] | Web/Empty |
ASP.NET Core Web App (Model-View-Controller) | mvc | [C#], F# | Web/MVC |
ASP.NET Core Web App (Razor Pages) | razor | [C#] | Web/MVC/Razor Pages |
ASP.NET Core with Angular | angular | [C#] | Web/MVC/SPA |
ASP.NET Core with React.js | react | [C#] | Web/MVC/SPA |
ASP.NET Core with React.js and Redux | reactredux | [C#] | Web/MVC/SPA |
ASP.NET Core Web API | webapi | [C#] | Web/WebAPI |
Nuget Config | nugetconfig | Config | |
Web Config | webconfig | Config | |
Solution File | sln | Solution | |
Razor Page | page | Web/ASP.NET | |
MVC ViewImports | viewimports | Web/ASP.NET | |
MVC ViewStart | viewstart | Web/ASP.NET |
利用好—help/-h 参数
#示例:
dotnet --help
dotnet new --help
dotnet restore --help
dotnet new console --help
dotnet new mvc --help