0

這裏是我的appsettings.json文件Configuration.GetConnectionString返回null

{ 
    "ConnectionStrings": { 
    "DefaultConnection": "Host=localhost;Port=5432;Database=db;User ID=postgres;Password=root" 
    }, 
    "Logging": { 
    "IncludeScopes": false, 
    "LogLevel": { 
     "Default": "Debug", 
     "System": "Information", 
     "Microsoft": "Information" 
    } 
    } 
} 

這是我如何檢索連接字符串:

// Only works when run through visual studio not on vs code 
Configuration.GetConnectionString("DefaultConnection") 

launch.json

{ 
    "version": "0.2.0", 
    "configurations": [ 
     { 
      "name": ".NET Core Launch (web)", 
      "type": "coreclr", 
      "request": "launch", 
      "preLaunchTask": "build", 
      "program": "${workspaceRoot}\\src\\Chlx\\bin\\Debug\\netcoreapp1.0\\Chlx.dll", 
      "args": [], 
      "cwd": "${workspaceRoot}", 
      "stopAtEntry": false, 
      "internalConsoleOptions": "openOnSessionStart", 
      "launchBrowser": { 
       "enabled": true, 
       "args": "${auto-detect-url}", 
       "windows": { 
        "command": "cmd.exe", 
        "args": "/C start ${auto-detect-url}" 
       }, 
       "osx": { 
        "command": "open" 
       }, 
       "linux": { 
        "command": "xdg-open" 
       } 
      }, 
      "env": { 
       "ASPNETCORE_ENVIRONMENT": "Development" 
      }, 
      "sourceFileMap": { 
       "/Views": "${workspaceRoot}/Views" 
      } 
     }, 
     { 
      "name": ".NET Core Attach", 
      "type": "coreclr", 
      "request": "attach", 
      "processId": "${command.pickProcess}" 
     } 
    ] 
} 

tasks.json

{ 
    "version": "0.1.0", 
    "command": "dotnet", 
    "isShellCommand": true, 
    "args": [], 
    "tasks": [ 
     { 
      "taskName": "build", 
      "args": [ 
       "${workspaceRoot}\\src\\Chlx\\project.json" 
      ], 
      "isBuildCommand": true, 
      "problemMatcher": "$msCompile" 
     } 
    ] 
} 

你知道如何解決這一問題?

+0

您是如何通過VS代碼「運行」它? – DavidG

+0

我通過調試窗格「.Net Core Launch(Web)」運行它。我可以設置斷點和獲取連接字符串的方法vs vs – bman

+0

如何在啓動時設置配置? –

回答

0

您從根(解決方案級別)運行程序,而不是從項目運行。將launchsettings.json中的「cwd」更改爲$ {workspaceRoot} \ src \ Chlx \

+0

你真棒 – bman

+0

或將applicationsettings.json添加到根文件夾。這爲我修復了:) –

+0

在ASP.NET Core 2.0中,我有兩個項目:WebApi和DataAccess。 WebApi包含appsettings.json。在父目錄(包含兩個項目)中,它是解決方案文件。我遇到了同樣的問題,並且:在解決方案級別複製appsettings.json或從WebApi項目運行應用程序,其中任何解決了我的問題 – hmadrigal