visual-studio-code - 在 vscode 中调试 .net core webapi 时无法读取 appsettings.json

标签 visual-studio-code asp.net-core-webapi vscode-debugger

在 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.jsonappsettings.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 的所有功劳...
enter image description here

关于visual-studio-code - 在 vscode 中调试 .net core webapi 时无法读取 appsettings.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63899926/

相关文章:

visual-studio-code - 如何阻止 VSCode 自动格式化删除括号内的空格?

asp.net-web-api - 如何在asp.net core webapi中上传带有其他模型属性的Iformfile?

c# - 如何将多部分/表单数据发送到 ASP.NET Core Web API?

python - 如何在使用 bazel 构建的项目中使用 vscode python 调试器?

visual-studio-code - 如何在 VSCode 中查找命令标识符?

regex - 在 Visual Studio Code 中搜索时匹配括号内的字符串

visual-studio-code - 使用 Visual Studio Code 的多个命令/任务

asp.net-core - 我们如何将 identityserver4 和 web api 集成到同一个项目(端口)中,而不是为每个项目创建不同的项目?

vscode-extensions - 无法解析 json 文件,可能是由于注释或尾随逗号所致

debugging - Flutter 将调试器输出附加到哪里?