配置.GetConnectionString在VS Code上运行asp.net core时返回null,但在Visua



这是我的应用设置。json 文件

{
  "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")
我的<<p> strong> 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}"
        }
    ]
}
我的<<p> strong> tasks.json
{
    "version": "0.1.0",
    "command": "dotnet",
    "isShellCommand": true,
    "args": [],
    "tasks": [
        {
            "taskName": "build",
            "args": [
                "${workspaceRoot}\src\Chlx\project.json"
            ],
            "isBuildCommand": true,
            "problemMatcher": "$msCompile"
        }
    ]
}

你知道怎么解决这个问题吗?

您从根(解决方案级别)而不是从项目运行程序。在启动设置中更改"cwd"。${workspaceRoot}srcChlx

Configuration属性在Startup类中定义,并通过依赖注入进行初始化。下面是使用。net创建的项目示例。new mvc -au Individual

  public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }

相关内容

  • 没有找到相关文章

最新更新