Mik*_*EEE 9 c# environment configuration json asp.net-core
我目前正在创建一个 Asp.NET Core 3.1 API 应用程序。在其中,我有一个launchSettings.json
如下所示:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:61392",
"sslPort": 44308
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"Key": "Value",
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Application.API": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
Run Code Online (Sandbox Code Playgroud)
特别是,我对获取applicationUrl
此文件中定义的值很感兴趣。不幸的是,除了通过 JSON 序列化直接加载文件并探测那里的值之外,我没有看到任何明显的方法来访问它们。
因此,我正在寻找更优雅的解决方案。有没有办法从Startup.Configure
和/或Startup.ConfigureServices
方法中访问这些值?
我在想类似的事情:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:61392",
"sslPort": 44308
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"Key": "Value",
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Application.API": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
Run Code Online (Sandbox Code Playgroud)
作为参考,我确实看到了这个问题,但这似乎需要一个 HTTP 请求。就我而言,在发出请求之前,我仍在配置服务器。
为了进一步参考和背景:我建立一个.NET GitHub的App应用,并正在使Smee.io客户端,其Javascript代码的对应的等效可以在这里找到。
客户端获取source
(通过服务器发送的事件)并将其发送到target
. 我已经source
弄清楚并配置了,但具有讽刺意味的是,在以明显和配置的方式访问和建立target
URL(applicationUrl
如上所示)时遇到了麻烦。
最后,我知道我可以在硬编码值appSettings.json
,但随后这将意味着我将在两个地方维护相同的值-一个在appSettings.json
,一个在已经存在的launchSettings.json
。如果可能的话,我宁愿把它放在一个地方以减少维护负担。
小智 12
我最近实际上也在寻找类似的东西。
事实证明,您可以通过查看环境变量来确定 Visual Studio 在调试会话期间将哪些 URL 传递给应用程序(从 launchSettings.json 中的 applicationUrl)。
尽管请记住,这可能仅适用于在使用 launchSettings.json 文件的 VStudio IDE 中进行调试
var urls = Environment.GetEnvironmentVariable("ASPNETCORE_URLS").Split(";");
Run Code Online (Sandbox Code Playgroud)
在你的Startup
构造函数中,你可以IConfiguration
从依赖注入中获得一个。这样,您就可以appsettings.json
使用如下配置来访问内部属性:
var appUrl = _configuration["profiles:applicationUrl"];
Run Code Online (Sandbox Code Playgroud)
如果您要访问内部的许多属性,appsettings.json
您可以使用该IConfiguration.Bind()
方法将配置 json 中的所有属性绑定到类实例。例如,您将有:
public class Config
{
public Profile Profiles { get; set; }
}
public class Profile
{
public string ApplicationUrl { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后在ConfigureServices
:
var config = new Config();
_configuration.Bind(config);
Run Code Online (Sandbox Code Playgroud)
然后,如果您将配置注册到服务提供者中,您可以通过 DI 在任何地方访问您的配置:
services.AddSingleton(config);
Run Code Online (Sandbox Code Playgroud)
编辑
要添加除默认 appsettings.json 之外的 json 文件,请在下面记录ConfigureAppConfiguration()
后调用 IWebHostBuilder 。CreateDefaultBuilder
Program.cs
在回调参数中,您可以将任意数量的 json、xml、ini 文件添加到您想要的配置中,然后使用我上面的答案从所有文件中获取任何值。请注意,如果存在两个相同的键,则将使用该键的最后一个值。
归档时间: |
|
查看次数: |
7488 次 |
最近记录: |