Visual studio 2015 在VS代码上运行asp.net core时,Configuration.GetConnectionString返回null,但在Visual Studio上则返回罚款
这是我的appsettings.json文件Visual studio 2015 在VS代码上运行asp.net core时,Configuration.GetConnectionString返回null,但在Visual Studio上则返回罚款,visual-studio-2015,asp.net-core,connection-string,visual-studio-code,npgsql,Visual Studio 2015,Asp.net Core,Connection String,Visual Studio Code,Npgsql,这是我的appsettings.json文件 { "ConnectionStrings": { "DefaultConnection": "Host=localhost;Port=5432;Database=db;User ID=postgres;Password=root" }, "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Debug", "System
{
"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")
Mylaunch.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}"
}
]
}
{
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [
"${workspaceRoot}\\src\\Chlx\\project.json"
],
"isBuildCommand": true,
"problemMatcher": "$msCompile"
}
]
}
Mytasks.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}"
}
]
}
{
"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\配置属性在Startup类中定义,并通过依赖项注入进行初始化。下面是使用dotnet新建mvc-au创建的项目的示例
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
您是如何通过VS代码“运行”它的?我是通过调试窗格“.Net核心启动(Web)”运行它的。我可以设置断点,并且获取连接字符串的方法在vs code上返回null。如何在启动时设置配置?请检查launchSettings.json中的
environmentVariables
。请检查我的编辑。我添加了launch.json和tasks.json。您是一个了不起的人,或者将applicationsettings.json添加到根文件夹中。在ASP.NETCore2.0中,我有两个项目:WebApi和DataAccess。WebApi包含appsettings.json。在父目录(包含两个项目)中,它是解决方案文件。我也经历过同样的问题:在解决方案级别复制appsettings.json,或者从WebApi项目运行app,所有这些都解决了我的问题