一、前言

二、安裝 .NET Core SDK

  • 1、安裝必要依賴
sudo yum -y install libunwind libicu
cd /home/downloads curl -sSL -o dotnet-2.0-preview.tar.gz https://aka.ms/dotnet-sdk-2.0.0-preview2-linux-x64-bin #preview版本下載地址需參考:https://www.microsoft.com/net/core/preview 
  • 3、創建SDK文件夾&將SDK解壓到指定文件夾中
sudo mkdir -p /usr/dotnet/dotnet-2.0-preview && sudo tar zxf dotnet-2.0-preview.tar.gz -C /usr/dotnet/dotnet-2.0-preview
  • 4、創建軟連接
sudo ln -s /usr/dotnet/dotnet-2.0-preview/dotnet /usr/local/bin 

三、熟悉命令(cmd)

  • 1、查看版本
dotnet --version
#2.0.0-preview2-006497 
  • 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、創建項目
#1、創建&打開項目文件夾 mkdir /projects && cd /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 /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 /projects/helloworld/obj/helloworld.csproj.nuget.g.props. Generating MSBuild file /projects/helloworld/obj/helloworld.csproj.nuget.g.targets. Restore completed in 3.75 sec for /projects/helloworld/helloworld.csproj. Restore succeeded. #項目文件就在/projects/helloworld/中 
  • 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 /projects/helloworld/helloworld.csproj... 
  • 3、運行
cd /projects/helloworld/ dotnet run #運行結果 Hello World! # 恭喜你,你的第一個.NET Core應用程序就這么誕生了 #源代碼請查看 /projects/helloworld/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

不得不說:C#才是.NET平台的親兒子啊

  • 如何知道命令支持哪些參數?
    答:利用好—help/-h 參數
#示例: dotnet --help dotnet new --help dotnet restore --help dotnet new console --help dotnet new mvc --help