.NET Core快速入门教程 3、我的第一个.NET Core App (CentOS篇)

一、前言

二、安装 .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

 
上一篇:Redux 入门教程(一):基本用法


下一篇:php实现工厂模式