dotnet 命令行
安装的SDKs
列出已经安装的SDK
dotnet --list-sdks
列出.NET 模板
dotnet new --list
Name | Description |
---|---|
web | 创建最小代码和内容的web程序 |
mvc | 创建mvc框架的web程序 |
webapp | 创建Razor页的web程序 |
blazorserver | 创建Blazor服务 |
angular | 创建包含前端的Angular javascript框架的web程序 |
react | 创建包含前端的React javasxript框架的web程序 |
reactredux | 创建包含前端的react javascript框架和redux库的wweb程序 |
配置项模板
Name | Description |
---|---|
globaljson | 添加一个global.json文件,指定项目的.net版本 |
sln | 创建一个解决方案文件,用来组合多个项目 |
gitignore | 创建.gitignore文件来指定从git中排除不需要的文件 |
创建一个新项目
dotnet new globaljson --sdk-version 6.0.100 --output MySolution/MyProject
dotnet new mvc --no-https --output MySolution/MyProject --framework net6.0
dotnet new sln -o MySolution
dotnet sln MySolution add MySolution/MyProject
运行
dotnet run
可以自动运行浏览器
dotnet watch
功能简介
Problem | Solution |
---|---|
Creating a project | dotnet new |
Building and running projects | dotnet build and dotnet run |
Adding packages to a project | dotnet add package |
Installing tool | dotnet tool |
Managing client-side packages | libman or Visual Studio client-side package manager |
管理包
管理NuGet包
安装包
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 6.0.0
从项目中移除包
dotnet remove package Microsoft.EntityFrameworkCore.SqlServer
列出项目中使用的包
dotnet list package
管理工具包
安装工具包
// --global参数会将工具包安装到所有项目,如果仅安装到指定包,可以使用dotnet tool run <command>
dotnet tool install --global dotnet-ef --version 6.0.0
dotnet tool uninstall --global dotnet-ef
管理前端包
前端包管理需要libraryManager(libMan)工具
安装LibMan工具包
dotnet tool uninstall --global Microsoft.Web.LibraryManager.Cli
dotnet tool install --global Microsoft.Web.LibraryManager.Cli --version 2.1.113
初始化项目
libman init -p cdnjs
安装bootstrap css框架
libman install bootstrap@5.1.3 -d wwwroot/lib/bootstrap
单元测试项目工具
单元测试项目
Name | Description |
---|---|
mstest | MS Test框架 |
nunit | Nunit框架 |
xunit | XUnit框架 |
创建UnitTest
dotnet new xunit -o SimpleApp.Tests --framework net6.0
dotnet sln add SimpleApp.Tests
dotnet add SimpleApp.Tests reference SimpleApp
移除默认的测试类
Remove-Item SimpleApp.Tests/UnitTest1.cs
运行测试程序
//进入项目单元测试文件夹,然后运行以下命令
dotnet test
使用单元测试的模拟包
dotnet add SimpleApp.Test package Moq --version 4.16.1
数据库操作
Installing Entity Framework Core
dotnet tool uninstall --global dotnet -ef
dotnet tool install --global dotnet-ef --version 6.0.0
Testing the Entity Framework Core Global Tool
dotnet ef --help
数据库迁移
dotnet ef migrations add Initial
Adding Entity Framework Core Packages to the Project
dotnet add package Microsoft.EntityFrameworkCore.Design --version 6.0.0
dotnet add package Microsoft.EntityFrameworkCore.SqlServe --version 6.0.0
重置数据库
dotnet ef database drop --force --context StoreDbContext
访问权限
安装Identity包
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore --version 6.0.0
迁移Identity数据库
dotnet ef migrations add Initial --context AppIdenttiyDbContext
更新Identity迁移
dotnet ef database update --context AppIdentityDbContext
删除和重新改变Identity数据库
dotnet ef database drop --force --context AppIdentityDbContext
存储敏感信息
Installing the User Secrets Tool Package
dotnet tool uninstall --global dotnet-user-secrets
dotnet tool install --global dotnet-users-secrets --version 3.0.0-preview-18579-0056
Initializing User Secrets
dotnet user-secrets init
生成json文件得路径”C:\Users{当前用户}\AppData\Roaming\Microsoft\UserSecrets\f133851b-2eeb-4685-a72b-9916c0439687″
Storing a userSecret
dotnet user-secrets set "WebService:Id" "MyAccount"
dotnet user-secrets set "WebService:Key" "MySecret123$"
Listing the User Secrets
dotnet user-secrets list
User Secrets are loaded only when the application is set to the Development environment.
https证书
Regeneration the Development Certificates
dotnet dev-certs https --clean
dotnet dev-certs https --trust
Filed under: ASP.NET Core,C#,编程 - @ 2022年4月12日 下午4:31