在 vscode 中调试时,我无法读取 appsettings.json
在我的 Startup.cs
我的 webapi 项目的文件。它们都返回为空/空。
我的 launch.json
webapi 的配置是通过添加 .NET Core Launch (console)
生成的配置。我也尝试添加 .NET Core Launch (web)
配置,但出现同样的问题。
我当时唯一需要改变的是 "program"
设置指向我的项目文件夹中正确的 dll。
这是我的 launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/SATestUtils.API/bin/Debug/netcoreapp3.1/SATestUtils.API.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"console": "internalConsole"
}
]
}
这是我的 tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/SATestUtils.Api/SATestUtils.API.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/SATestUtils.Api/SATestUtils.API.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/SATestUtils.Api/SATestUtils.API.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
这是我的 appsettings.json
和 appsettings.Development.json
文件...{
"ConnectionStrings": {
"DefaultConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=SaTestUtils"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"StorageAccountName": "devstoreaccount1",
"AzureAD": {
"Instance": "https://login.microsoftonline.com",
"Domain": "[Domain]",
"TenantId": "[TenantId]",
"ClientId": "[ClientId]",
"Scope": "[Scope]"
},
"AllowedHosts": "*"
}
当我尝试在 Startup.cs
中的服务方法中调试以下代码时var connectionString = Configuration["ConnectionStrings:DefaultConnection"];
Console.WriteLine($"Connection string = {connectionString}");
我只是将以下内容记录到调试终端Connection string =
appsettings.json
中的所有其他设置也不回来。使用 dotnet run
时返回正确的设置.这里可能是什么问题?
请注意,我的工作目录中有一个解决方案文件,然后我有一个名为
SATestUtils.API
的文件夹。包含 SATestUtils.API.csproj
我的 webapi 项目的文件。
最佳答案
我遇到了同样的情况,对我来说,有效的是之前评论过的:
将 launch.json 中 'cwd' 属性的值更改为项目目录的值。
{
...
"cwd": "${workspaceFolder}"
}
到
{
...
"cwd": "${workspaceFolder}/SATestUtils.API"
}
Bemm 的所有功劳...
关于visual-studio-code - 在 vscode 中调试 .net core webapi 时无法读取 appsettings.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63899926/