我无法让我Configuration.GetSection
返回数据.Value
。我想我实施了这个问题的所有建议,但仍然无法让它发挥作用。
应用设置.json
{
"AmazonSettings": {
"BaseUrl": "https://testing.com",
"ClientID": "123456",
"ResponseType": "code",
"RedirectUri": "https://localhost:44303/FirstTimeWelcome"
},
}
启动:
public IConfiguration Configuration { get; }
public Startup(IHostingEnvironment env)
{
//Set up configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
配置服务:
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<AmazonSettings>(Configuration.GetSection("AmazonSettings"));
services.AddMvc()
亚马逊设置类:
public class AmazonSettings
{
public string BaseUrl { get; set; }
public string ClientID { get; set; }
public string RedirectUri { get; set; }
public string ResponseType { get; set; }
}
我正在尝试通过 IOptions 访问 AmazonSettings.Value:
public class HomeController : Controller
{
private readonly AmazonSettings _amazonSettings;
public IActionResult Index()
{
ViewBag.LoginUrl = _amazonSettings.BaseUrl;
return View("/Pages/Index.cshtml"); ;
}
public HomeController(IOptions<AmazonSettings> amazonSettings)
{
_amazonSettings = amazonSettings.Value;
}
当我调试时,值为空: