使用VS Code 进行.NET 开发

今天我们就简单介绍如何使用vs code 进行.NET 开发,请自行安装 .NET6 等必要的编译环境。用vs code 进行.NET 开发,需要用到几个组件。

一、C#

二、Auto-Using for C#

 

三、vscode-solution-explorer 

四、NuGet Package Manager GUI

五:C# Extensions

 安装以上插件后,运行VS Code:

 选择文件夹之后,即可打开现有项目了

VS Code 新建终端:

运行命令:dotnet --version,如下图:

 生成项目:

 dotnet run --project xxxntry.csproj 

注意:运行上面命令,需在当前xxxntry.csproj所在目录执行,你可以使用cd 命令进入目录:cd xxxntry

生成成功

在浏览器输入此地址即可打开项目:

我这只是测试代码webapi,集成swagger,所以打开为swagger地址

同理,你可以使用dotnet build命令编译:

Ctrl+C 停止运行

常用命令如下:

一.创建解决方案:

#创建解决方案 sln
dotnet new sln -n xxx

二.创建项目:

# 创建类库项目
dotnet new classlib -n xxx.Common

三.创建控制台应用程序

# 创建控制台应用程序
dotnet new console -n xxx.win

四.创建测试

# 创建xUnit单元测试项目
dotnet new xunit -n xxx.tests

五.添加引用和nuget引用

# 为 Tests 添加 Core 引用
dotnet add xxx.tests reference xxx.Common

# 为 项目添加 Nuget 引用
dotnet add xxx.Common package Hash --version 4.0.0

六.编译项目

#编译项目
dotnet build xxx.Common

七.单元测试

#执行单元测试,执行所有方法
dotnet test xxx.tests

#执行单元测试,指定的方法
dotnet test
xxx.tests --filter getUserName

八.运行项目

#运行
dotnet run --project xxx.win

九.发布项目

# 发布Release配置,包括 .net core 运行时,分别发布到 linux 和 windows
dotnet publish -c Release --self-contained -r linux-x64
dotnet publish -c Release --self-contained -r win-x64

# 发布Release配置,包括 .net core 运行时,指定目标框架 netcoreapp2.2
dotnet publish -c Release -f netcoreapp2.2 --self-contained -r linux-x64
dotnet publish -c Release -f netcoreapp2.2 --self-contained -r win-x64

dotnet run -c Release-f net6.0--runtimes net6.0  --self-contained -r win-x64 --未测试

# 发布Release配置,不包括 .net core 运行时
dotnet publish -c Release --self-contained false -r linux-x64
dotnet publish -c Release --self-contained false -r win-x64

# 发布Release配置,不包括 .net core 运行时,指定输出目录
dotnet publish -c Release --self-contained false -r linux-x64 -o C:publishlinux-x64
dotnet publish -c Release --self-contained false -r win-x64 -o C:publishwin-x64

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>