tarathec tutorial backend
0.0.1
ASP.NET 是一个开源 Web 框架,由 Microsoft 创建,用于使用 .NET 构建现代 Web 应用程序和服务。 ASP.NET 是跨平台的,可以在 Windows、Linux、macOS 和 Docker 上运行。
更多:https://dotnet.microsoft.com/en-us/learn/aspnet/what-is-aspnet
创建解决方案项目
dotnet new sln -n Tutorial-Api
创建 ASP.NET Web Api 项目
dotnet new webapi -o Api
创建XUnit项目
dotnet new xunit -o Xunit.Tests
将项目添加到解决方案
dotnet sln add .Tutorial.ApiTutorial.Api.csproj
dotnet sln add .XUnit.TestsXUnit.Tests.csproj
将参考项目添加到xunit项目中
dotnet add . /XUnit.Tests/XUnit.Tests.csproj reference .Tutorial.ApiTutorial.Api.csproj
将依赖 MongoDB Driver 添加到项目中
dotnet add package MongoDB.Driver --version 2.18.0
蒙戈壳
Install mongo-shell
https://www.mongodb.com/docs/mongodb-shell/install/
> mongod --dbpath < data_directory_path >
> show dbs
> use tutorialdb
> db.createCollection( ' tutorials ' )
> db.tutorials.insertMany([{ " title " : " Design Patterns " , " description " : " " , " published " : false}])
> db.tutorials.find().pretty ()
dotnet run --project . /Tutorial.Api/
招摇:https://localhost:7272/swagger
https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-run#examples
获取/api/教程
回复
[
{
"id" : " 63730beabd3cb05f2331be45 " ,
"title" : " hello " ,
"description" : " world " ,
"published" : null ,
"createdAt" : " 0001-01-01T00:00:00Z " ,
"updatedAt" : " 0001-01-01T00:00:00Z "
},
{
"id" : " 63732124796b18bc753e9157 " ,
"title" : " dotnet " ,
"description" : " microsoft " ,
"published" : null ,
"createdAt" : " 0001-01-01T00:00:00Z " ,
"updatedAt" : " 0001-01-01T00:00:00Z "
}
]
获取 /api/tutorials/{id}
回复
{
"id" : " 63730beabd3cb05f2331be45 " ,
"title" : " hello " ,
"description" : " world " ,
"published" : null ,
"createdAt" : " 0001-01-01T00:00:00Z " ,
"updatedAt" : " 0001-01-01T00:00:00Z "
}
POST /api/教程
请求正文
{
"title" : " string " ,
"description" : " string " ,
}
响应体
{
"code" : " 200 " ,
"message" : " Inserted a single document Success "
}
删除/api/教程
响应体
{
"code" : " 200 " ,
"message" : " All deleted "
}
删除 /api/tutorials/{id}
响应体
{
"code" : " 200 " ,
"message" : " Deleted id 63730beabd3cb05f2331be45 "
}
PUT /api/tutorials/{id}
请求正文
{
"id" : " 63730beabd3cb05f2331be45 " ,
"title" : " hello " ,
"description" : " world " ,
"published" : true
}
响应体
{
"code" : " 200 " ,
"message" : " Deleted id 63730beabd3cb05f2331be45 "
}
dotnet build
恢复为不同的图层
dotnet restore
构建并发布版本
dotnet publish -c Release -o out
运行覆盖范围
dotnet test --collect:"XPlat Code Coverage"
添加报告生成器 nuget
dotnet add package ReportGenerator --version 5.1.10
设置工具ReportGenerator
dotnet tool install -g dotnet-reportgenerator-globaltool
更多:https://www.nuget.org/packages/ReportGenerator
reportgenerator -reports:"XUnit.TestsTestResults*coverage.cobertura.xml" -targetdir:". /coveragereport" -reporttypes:Html
环境变量
环境名称 | 价值 |
---|---|
教程数据库__ConnectionString | mongodb://xxxxxxxx |
教程数据库__数据库名称 | 教程数据库 |
教程数据库__TutorialCollectionName | 教程 |
https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger#aspnet-core-applications
添加包 ApplicationInsights
dotnet add package Microsoft.ApplicationInsights.AspNetCore --version 2.21.0
...