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
…