Configuration.GetConnectionString returns null when 运行 asp.net VS Code 的核心但在 Visual Studio 上很好
Configuration.GetConnectionString returns null when running asp.net core on VS Code but fine on Visual Studio
这是我的appsettings.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")
我的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"
}
]
}
你知道如何解决这个问题吗?
您 运行 来自根目录(解决方案级别)的程序,而不是来自项目的程序。将 launchsettings.json 中的 "cwd" 更改为 ${workspaceRoot}\src\Chlx\
配置 属性 在启动 class 中定义,并通过依赖注入进行初始化。这是使用 dotnet new mvc -au Individual
创建的项目示例
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
这是我的appsettings.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")
我的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"
}
]
}
你知道如何解决这个问题吗?
您 运行 来自根目录(解决方案级别)的程序,而不是来自项目的程序。将 launchsettings.json 中的 "cwd" 更改为 ${workspaceRoot}\src\Chlx\
配置 属性 在启动 class 中定义,并通过依赖注入进行初始化。这是使用 dotnet new mvc -au Individual
创建的项目示例 public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }