在配置文件中,我有
"HRServices": {
"CarService": "https://stackoverflow.com/Cars/...",
"EmployeesService": "https://stackoverflow.com/Employees/...",
"FinanceService": "https://stackoverflow.com/Finance/...",
....
}
是否可以为所有这些地址指定基本网址?
获得类似
"baseUrl":"https://stackoverflow.com",
"HRServices": {
"CarService": "baseUrl/Cars/...",
"EmployeesService": "baseUrl/Employees/...",
"FinanceService": "baseUrl/Finance/...",
....
}
更新:
它在控制器中使用过:
private readonly IConfiguration _configuration;
public HomeController(IConfiguration configuration)
{
_configuration = configuration;
}
[HttpGet]
public PartialViewResult Index()
{
string url = _configuration["HRServices:EmployeesService"];
...
}
using System.Collections.Generic;
namespace Microsoft.Extensions.Configuration
{
//
// Summary:
// Represents the root of an Microsoft.Extensions.Configuration.IConfiguration hierarchy.
public interface IConfigurationRoot : IConfiguration
{
//
// Summary:
// The Microsoft.Extensions.Configuration.IConfigurationProviders for this configuration.
IEnumerable<IConfigurationProvider> Providers { get; }
//
// Summary:
// Force the configuration values to be reloaded from the underlying Microsoft.Extensions.Configuration.IConfigurationProviders.
void Reload();
}
}
答案 0 :(得分:1)
没有什么可以自动为您执行此操作,但是您可以手动注册您的强类型配置,然后自己进行:
services.AddScoped(p =>
{
var config = p.GetRequiredService<IConfiguration>();
var baseUrl = config["baseUrl"]
return new HRServicesConfig
{
CarService = baseUrl + config["HRServices:CarService"],
EmployeeService = baseUrl + config["HRServices:EmployeeService"],
FinanceService = baseUrl + config["HRServices:FinanceService"]
}
});
一个要注意的是,您将不再使用IOptions
,因此您将直接注入HRServicesConfig
,而不是类似IOptions<HRServicesConfig>
的注入。不过,有些人可能会认为该功能。