[英]ASP.NET core 3.1: does ConfigureAppConfiguration interacts with launchSettings.json?
[英]How Do You Access the `applicationUrl` Property Found in launchSettings.json from Asp.NET Core 3.1 Startup class?
我目前正在創建一個 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"
}
}
}
特別是,我對獲取此文件中定義的applicationUrl
值很感興趣。 不幸的是,除了通過 JSON 序列化直接加載文件並探測那里的值之外,我沒有看到任何明顯的方法來訪問它們。
因此,我正在尋找更優雅的解決方案。 有沒有辦法從Startup.Configure
和/或Startup.ConfigureServices
方法中訪問這些值?
我在想類似的事情:
public void Configure(IApplicationBuilder builder)
{
var url = builder.ApplicationServices.GetRequiredService<IServerHostingEnvironment>().ApplicationUrl
}
作為參考,我確實看到了這個問題,但這似乎需要一個 HTTP 請求。 就我而言,在發出請求之前,我仍在配置服務器。
進一步參考和上下文:我正在構建一個 .NET GitHub App 應用程序,並且正在制作 Smee.io 客戶端的相應等價物,其 Javascript 代碼可以在這里找到。
客戶端獲取source
(通過服務器發送的事件)並將其發送到target
。 我找到並配置了source
,但具有諷刺意味的是,在以明顯的配置方式訪問和建立target
URL(上面看到的applicationUrl
)時遇到了麻煩。
最后,我意識到我可以在appSettings.json
對值進行硬編碼,但這意味着我將在兩個地方維護相同的值——一個在appSettings.json
,另一個在已經存在的launchSettings.json
。 如果可能的話,我寧願把它放在一個地方以減少維護負擔。
尚未測試,但我使用此模式訪問 appsettings.json。
在 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; }
將其添加為服務:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton <IConfiguration> (Configuration);
}
將其注入控制器並獲取鍵值:
public class SomeController: ControllerBase {
private IConfiguration _iconfiguration;
public SomeController(IConfiguration iconfiguration) {
_iconfiguration = iconfiguration;
}
void SomeMethod(){
string appUrl = _iConfiguration.GetValue<string>("applicationUrl","someDefaultValue")
} }
希望這可以幫助你!
實際上,我最近也在尋找類似的東西。
事實證明,您可以通過查看環境變量來確定在調試會話期間 Visual Studio 將哪些 URL 傳遞給應用程序(來自 launchSettings.json 中的 applicationUrl)。
盡管記住這可能僅對使用 launchSettings.json 文件的 VStudio IDE 中的調試有用
var urls = Environment.GetEnvironmentVariable("ASPNETCORE_URLS").Split(";");
在您的Startup
構造函數中,您可以從依賴注入中獲取IConfiguration
。 有了這個,您可以使用如下配置訪問appsettings.json
的屬性:
var appUrl = _configuration["profiles:applicationUrl"];
如果您將訪問appsettings.json
許多屬性,則可以使用IConfiguration.Bind()
方法將配置 json 中的所有屬性綁定到類實例。 例如,您將擁有:
public class Config
{
public Profile Profiles { get; set; }
}
public class Profile
{
public string ApplicationUrl { get; set; }
}
然后在ConfigureServices
:
var config = new Config();
_configuration.Bind(config);
然后,如果您將配置注冊到 serviceprovider 中,您就可以通過 DI 在任何地方訪問您的配置:
services.AddSingleton(config);
編輯
要添加默認 appsettings.json 以外的 json 文件,請在下面記錄的Program.cs
CreateDefaultBuilder
之后調用 IWebHostBuilder 上的ConfigureAppConfiguration()
。
在回調參數中,您可以將任意數量的 json、xml、ini 文件添加到您想要的配置中,然后使用我上面的回答從所有文件中獲取任何值。 請注意,如果存在兩個相同的鍵,則將使用該鍵的最后一個值。
我知道,一年過去了,但這個解決方案可能對某人有用:
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();
var appUrl = configuration["ASPNETCORE_URLS"].Split(";").First();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.