ASP.NET adalah kerangka web sumber terbuka, dibuat oleh Microsoft, untuk membangun aplikasi dan layanan web modern dengan .NET. ASP.NET bersifat lintas platform dan berjalan di Windows, Linux, macOS, dan Docker.
selengkapnya: https://dotnet.microsoft.com/en-us/learn/aspnet/what-is-aspnet
Buat proyek solusi
dotnet new sln -n Tutorial-Api
Buat proyek ASP.NET Web Api
dotnet new webapi -o Api
Buat proyek XUnit
dotnet new xunit -o Xunit.Tests
Tambahkan proyek ke solusi
dotnet sln add .Tutorial.ApiTutorial.Api.csproj
dotnet sln add .XUnit.TestsXUnit.Tests.csproj
Tambahkan proyek referensi ke proyek xunit
dotnet add . /XUnit.Tests/XUnit.Tests.csproj reference .Tutorial.ApiTutorial.Api.csproj
Tambahkan ketergantungan Driver MongoDB ke dalam proyek
dotnet add package MongoDB.Driver --version 2.18.0
kulit luwak
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/
Kesombongan : https://localhost:7272/swagger
https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-run#examples
DAPATKAN /api/tutorial
Tanggapan
[
{
"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 "
}
]
DAPATKAN /api/tutorial/{id}
Tanggapan
{
"id" : " 63730beabd3cb05f2331be45 " ,
"title" : " hello " ,
"description" : " world " ,
"published" : null ,
"createdAt" : " 0001-01-01T00:00:00Z " ,
"updatedAt" : " 0001-01-01T00:00:00Z "
}
POST /api/tutorial
Badan Permintaan
{
"title" : " string " ,
"description" : " string " ,
}
Badan Respon
{
"code" : " 200 " ,
"message" : " Inserted a single document Success "
}
HAPUS /api/tutorials
Badan Respon
{
"code" : " 200 " ,
"message" : " All deleted "
}
HAPUS /api/tutorials/{id}
Badan Respon
{
"code" : " 200 " ,
"message" : " Deleted id 63730beabd3cb05f2331be45 "
}
TEMPATKAN /api/tutorial/{id}
Badan Permintaan
{
"id" : " 63730beabd3cb05f2331be45 " ,
"title" : " hello " ,
"description" : " world " ,
"published" : true
}
Badan Respon
{
"code" : " 200 " ,
"message" : " Deleted id 63730beabd3cb05f2331be45 "
}
dotnet build
Pulihkan sebagai lapisan yang berbeda
dotnet restore
Buat dan publikasikan rilis
dotnet publish -c Release -o out
Jalankan Cakupan
dotnet test --collect:"XPlat Code Coverage"
Tambahkan nuget ReportGenerator
dotnet add package ReportGenerator --version 5.1.10
Alat penyiapan ReportGenerator
dotnet tool install -g dotnet-reportgenerator-globaltool
selengkapnya: https://www.nuget.org/packages/ReportGenerator
reportgenerator -reports:"XUnit.TestsTestResults*coverage.cobertura.xml" -targetdir:". /coveragereport" -reporttypes:Html
Variabel lingkungan
Nama Env | Nilai |
---|---|
TutorialDatabase__ConnectionString | mongodb://xxxxxxx |
TutorialDatabase__NamaDatabase | tutorialdb |
TutorialDatabase__TutorialCollectionName | tutorial |
https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger#aspnet-core-applications
Tambahkan paket ApplicationInsights
dotnet add package Microsoft.ApplicationInsights.AspNetCore --version 2.21.0
...