[英]How Do You Access the `applicationUrl` Property Found in launchSettings.json from Asp.NET Core 3.1 Startup class?
I am currently creating an Asp.NET Core 3.1 API Application.我目前正在创建一个 Asp.NET Core 3.1 API 应用程序。 In it, I have a
launchSettings.json
that looks like the following:在其中,我有一个
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"
}
}
}
In particular, I am interested in obtaining the applicationUrl
values defined in this file.特别是,我对获取此文件中定义的
applicationUrl
值很感兴趣。 Unfortunately, I do not see any obvious way to access them outside of loading the file directly via JSON serialization and probing the values there.不幸的是,除了通过 JSON 序列化直接加载文件并探测那里的值之外,我没有看到任何明显的方法来访问它们。
As such, I am looking for a more elegant solution.因此,我正在寻找更优雅的解决方案。 Is there a way to access these values from within the
Startup.Configure
and/or Startup.ConfigureServices
methods?有没有办法从
Startup.Configure
和/或Startup.ConfigureServices
方法中访问这些值?
I am thinking of something like:我在想类似的事情:
public void Configure(IApplicationBuilder builder)
{
var url = builder.ApplicationServices.GetRequiredService<IServerHostingEnvironment>().ApplicationUrl
}
For reference, I did see this question , but it seems that this requires an HTTP request.作为参考,我确实看到了这个问题,但这似乎需要一个 HTTP 请求。 In my case, I am still configuring the server before a request has been made.
就我而言,在发出请求之前,我仍在配置服务器。
For further reference and context: I am building a .NET GitHub App application, and am making the corresponding equivalent of the Smee.io client, whose Javascript code can be found here .进一步参考和上下文:我正在构建一个 .NET GitHub App 应用程序,并且正在制作 Smee.io 客户端的相应等价物,其 Javascript 代码可以在这里找到。
The client takes a source
(via Server-sent events) and sends it to a target
.客户端获取
source
(通过服务器发送的事件)并将其发送到target
。 I got the source
figured out and configured, but am ironically having trouble accessing and establishing the target
URL (the applicationUrl
seen above) in an obvious and configured fashion.我找到并配置了
source
,但具有讽刺意味的是,在以明显的配置方式访问和建立target
URL(上面看到的applicationUrl
)时遇到了麻烦。
Finally, I realize I could hardcode the value in appSettings.json
, but that would then mean I would be maintaining the same value in two places -- one in appSettings.json
and one in the already-existing launchSettings.json
.最后,我意识到我可以在
appSettings.json
对值进行硬编码,但这意味着我将在两个地方维护相同的值——一个在appSettings.json
,另一个在已经存在的launchSettings.json
。 I would rather keep it to one place to reduce maintenance burden, if possible.如果可能的话,我宁愿把它放在一个地方以减少维护负担。
Have not tested it but I use this pattern to access appsettings.json.尚未测试,但我使用此模式访问 appsettings.json。
In startup.cs add launchsettings.json to the configuration builder:在 startup.cs 中将 launchsettings.json 添加到配置构建器:
public Startup(IConfiguration configuration)
{
var builder = new ConfigurationBuilder().AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "Properties", "launchSettings.json"));
Configuration = builder.Build();
}
public IConfiguration Configuration { get; }
Add it as a service:将其添加为服务:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton <IConfiguration> (Configuration);
}
Inject it in controller and get key value:将其注入控制器并获取键值:
public class SomeController: ControllerBase {
private IConfiguration _iconfiguration;
public SomeController(IConfiguration iconfiguration) {
_iconfiguration = iconfiguration;
}
void SomeMethod(){
string appUrl = _iConfiguration.GetValue<string>("applicationUrl","someDefaultValue")
} } } }
Hope this helps you out!希望这可以帮助你!
I was actually looking for something similar myself recently.实际上,我最近也在寻找类似的东西。
It turns out you can determine what URL's were passed to the application by Visual Studio during a debugging session (from applicationUrl within launchSettings.json) By looking at the environmental variables.事实证明,您可以通过查看环境变量来确定在调试会话期间 Visual Studio 将哪些 URL 传递给应用程序(来自 launchSettings.json 中的 applicationUrl)。
Although bearing in mind this is probably only useful for debugging within VStudio IDE which uses the launchSettings.json file尽管记住这可能仅对使用 launchSettings.json 文件的 VStudio IDE 中的调试有用
var urls = Environment.GetEnvironmentVariable("ASPNETCORE_URLS").Split(";");
In your Startup
constructor, you can get an IConfiguration
from dependency injection.在您的
Startup
构造函数中,您可以从依赖注入中获取IConfiguration
。 With this, you can access properties inside your appsettings.json
by using the configuration like so:有了这个,您可以使用如下配置访问
appsettings.json
的属性:
var appUrl = _configuration["profiles:applicationUrl"];
If you will be accessing many properties inside your appsettings.json
you can use the IConfiguration.Bind()
method to bind all properties within your configuration json to a class instance.如果您将访问
appsettings.json
许多属性,则可以使用IConfiguration.Bind()
方法将配置 json 中的所有属性绑定到类实例。 For instance, you would have:例如,您将拥有:
public class Config
{
public Profile Profiles { get; set; }
}
public class Profile
{
public string ApplicationUrl { get; set; }
}
Then in ConfigureServices
:然后在
ConfigureServices
:
var config = new Config();
_configuration.Bind(config);
Then if you register config into the serviceprovider you can access your configuration anywhere through DI:然后,如果您将配置注册到 serviceprovider 中,您就可以通过 DI 在任何地方访问您的配置:
services.AddSingleton(config);
EDIT编辑
To add json files other than the default appsettings.json, call ConfigureAppConfiguration()
on the IWebHostBuilder after CreateDefaultBuilder
in Program.cs
documented below.要添加默认 appsettings.json 以外的 json 文件,请在下面记录的
Program.cs
CreateDefaultBuilder
之后调用 IWebHostBuilder 上的ConfigureAppConfiguration()
。
Link to File Configuration Docs 链接到文件配置文档
In the callback parameter you can add as many json,xml,ini files into the configuration you want, then use my answer above to get any values from all files.在回调参数中,您可以将任意数量的 json、xml、ini 文件添加到您想要的配置中,然后使用我上面的回答从所有文件中获取任何值。 Note that if two of the same key is present, the last value for that key will be used.
请注意,如果存在两个相同的键,则将使用该键的最后一个值。
I know, one year has passed, but this solution may be useful for somebody:我知道,一年过去了,但这个解决方案可能对某人有用:
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();
var appUrl = configuration["ASPNETCORE_URLS"].Split(";").First();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.