C# 在appsettings.json中读取ASP.NetCore 2.2
ASP.NetCore 2.2,VisualStudio 2019 我正在试图找出如何将web.configfile转换为appsettings.json,但我缺少一些东西 我有一个startup.cs文件,看起来像是为了简洁而编辑的:C# 在appsettings.json中读取ASP.NetCore 2.2,c#,asp.net-core,C#,Asp.net Core,ASP.NetCore 2.2,VisualStudio 2019 我正在试图找出如何将web.configfile转换为appsettings.json,但我缺少一些东西 我有一个startup.cs文件,看起来像是为了简洁而编辑的: using System; using MyCoolApp.Models.Commodities; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Mic
using System;
using MyCoolApp.Models.Commodities;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Rewrite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace MyCoolApp {
public class Startup {
public Startup(IHostingEnvironment env) {
var builder = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
private IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services) {
services.AddDistributedMemoryCache();
services.Configure<IISServerOptions>(
options => {
options.AutomaticAuthentication = false;
}
);
services.AddMvc().SetCompatibilityVersion(
CompatibilityVersion.Version_2_2
);
var foo = Configuration.GetConnectionString("CommoditiesContext");
services.AddDbContext<CommoditiesContext>(
options => options.UseSqlServer(
Configuration.GetConnectionString("CommoditiesContext")
)
);
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
...
}
}
}
我已将其转换为顶级appsettings.json文件:
我有一个appsettings.Development.json文件,看起来像:
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"location": {
"path": ".",
"inheritInChildApplications": "false",
"system.webServer": {
"handlers": [],
"aspNetCore": {
"processPath": "%LAUNCHER_PATH%",
"arguments": "%LAUNCHER_ARGS%",
"stdoutLogEnabled": "true",
"stdoutLogFile": ".\\logs\\stdout",
"hostingModel": "InProcess",
"environmentVariables": [
{
"name": "ASPNETCORE_HTTPS_PORT",
"value": "44375"
},
{
"name": "ASPNETCORE_ENVIRONMENT",
"value": "Development"
},
{
"name": "COMPLUS_ForceENC",
"value": "1"
}
],
"handlerSettings": [
{
"name": "debugFile",
"value": "aspnetcore-debug.log"
},
{
"name": "debugLevel",
"value": "FILE,TRACE"
}
]
},
"modules": [],
"isapiFilters": []
}
}
}
我也有一个制作版,但现在让我们限制噪音
现在,我的问题是,当我在本地调试时,我可以在startup.cs中的services.AddDbContext行上设置一个bp,然后查看foo的值。它是空的。我要么构造了不正确的appsettings.json文件,要么读取了不正确的配置信息,但我不知道是哪个
我错过了什么
编辑
program.cs
问题是您在启动构造函数中为配置设置了错误的基本路径。AppDomain.CurrentDomain.BaseDirectory将为您提供错误的位置,因此不会加载任何配置文件。相反,考虑使用St.I..Distury.GETCurrnTraseToel.< P>问题是,在启动构造函数中为配置设置错误的基本路径。AppDomain.CurrentDomain.BaseDirectory将为您提供错误的位置,因此不会加载任何配置文件。取而代之的是,考虑使用St.I..Distury.GETCurrnTrror目录。< /P> < P>从代码启动中取出这个代码。
var builder = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
.AddEnvironmentVariables();
Configuration = builder.Build();
编辑:正如柯克在评论中指出的那样
你需要这样替换它
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
为什么??因为如果你的Program.cs类中有
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
那你就不需要为自己设定目标了
基于ASP.NET核心dotnet新建的默认配置Web应用程序
模板在生成主机时调用CreateDefaultBuilder。
CreateDefaultBuilder为中的应用程序提供默认配置
顺序如下:
以下内容适用于使用Web主机的应用程序。有关
使用通用主机时的默认配置,请参阅最新版本
本主题的版本
主机配置由以下内容提供:前缀为环境变量
例如,对于ASPNETCORE_,ASPNETCORE_环境使用
环境变量配置提供程序。前缀为ASPNETCORE_
在加载配置键值对时剥离。
使用命令行配置提供程序的命令行参数。
应用程序配置由以下文件提供:appsettings.json
配置提供程序。appsettings.{Environment}.json使用该文件
配置提供程序。应用程序在中运行时的机密管理器
使用入口程序集的开发环境。环境
使用环境变量配置提供程序的变量。
使用命令行配置提供程序的命令行参数
从你的代码startup.cs中取出这个
var builder = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
.AddEnvironmentVariables();
Configuration = builder.Build();
编辑:正如柯克在评论中指出的那样
你需要这样替换它
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
为什么??因为如果你的Program.cs类中有
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
那你就不需要为自己设定目标了
基于ASP.NET核心dotnet新建的默认配置Web应用程序
模板在生成主机时调用CreateDefaultBuilder。
CreateDefaultBuilder为中的应用程序提供默认配置
顺序如下:
以下内容适用于使用Web主机的应用程序。有关
使用通用主机时的默认配置,请参阅最新版本
本主题的版本
主机配置由以下内容提供:前缀为环境变量
例如,对于ASPNETCORE_,ASPNETCORE_环境使用
环境变量配置提供程序。前缀为ASPNETCORE_
在加载配置键值对时剥离。
使用命令行配置提供程序的命令行参数。
应用程序配置由以下文件提供:appsettings.json
配置提供程序。appsettings.{Environment}.json使用该文件
配置提供程序。应用程序在中运行时的机密管理器
使用入口程序集的开发环境。环境
使用环境变量配置提供程序的变量。
使用命令行配置提供程序的命令行参数
如果我删除了你建议的部分,那么我就处于同一条船上:appsettings没有被读取。如果我将该部分保留在中并添加@DavidG,则表明我确实看到了我正在寻找的连接字符串;您还需要将IConfiguration注入到启动中,并将其分配给您的配置属性。如果我删除了您建议的部分,那么我将处于同一条船上:appsettings未被读取。如果我将该部分保留在中并添加@DavidG,则表明我确实看到了我正在寻找的连接字符串;您还需要将IConfiguration注入到启动中,并将其分配给您的配置属性。FWIW,您的appsettings.Development.json完全是我们的
毫无意义。web.config和appsettings.json之间没有一对一的关联。位置指令之类的东西不适用,system.webServer之类的部分也不适用。JSON只是数据,配置提供程序只需将所有这些转换为键值对的扁平字典,其中包含location:system.webServer:aspNetCore:processPath等键。就这样。这些实际上不会有任何效果。它们只是静态设置。呃,.NetCore不是我的朋友。我发现没有1:1的相关性,但如果有提供的转换工具或更好的文档或其他东西就好了。问题是这些都是IIS设置,ASP.NET Core不需要IIS,所以它们是单独配置的。appsettings.json等用于应用程序配置;对于IIS configuration.FWIW,您的appsettings.Development.json是完全无用的。web.config和appsettings.json之间没有一对一的关联。位置指令之类的东西不适用,system.webServer之类的部分也不适用。JSON只是数据,配置提供程序只需将所有这些转换为键值对的扁平字典,其中包含location:system.webServer:aspNetCore:processPath等键。就这样。这些实际上不会有任何效果。它们只是静态设置。呃,.NetCore不是我的朋友。我发现没有1:1的相关性,但如果有提供的转换工具或更好的文档或其他东西就好了。问题是这些都是IIS设置,ASP.NET Core不需要IIS,所以它们是单独配置的。appsettings.json等用于应用程序配置;不适用于IIS配置。