[英]Configuration.GetSection returns null value
I cannot get my Configuration.GetSection
to return data in .Value
. 我无法让我的
Configuration.GetSection
返回.Value
数据。 I think I implemented all the suggestions from this question , but still can't get it to work. 我想我已经实现了这个问题的所有建议,但仍然无法让它发挥作用。
appsettings.json appsettings.json
{
"AmazonSettings": {
"BaseUrl": "https://testing.com",
"ClientID": "123456",
"ResponseType": "code",
"RedirectUri": "https://localhost:44303/FirstTimeWelcome"
},
}
Startup: 启动:
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();
}
ConfigurationServices: ConfigurationServices:
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<AmazonSettings>(Configuration.GetSection("AmazonSettings"));
services.AddMvc()
AmazonSettings Class: AmazonSettings类:
public class AmazonSettings
{
public string BaseUrl { get; set; }
public string ClientID { get; set; }
public string RedirectUri { get; set; }
public string ResponseType { get; set; }
}
I'm attempting to access AmazonSettings.Value via IOptions: 我正在尝试通过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;
}
When I debug, Value is empty: 当我调试时,Value为空:
My problem was that my code in HomeController was never getting hit. 我的问题是我的HomeController中的代码从未受到过攻击。
I could get there, and .Value was populated, if I added Routes["home"] above my controller and navigated to localhost/home. 我可以到达那里,并且.Value已填充,如果我在控制器上方添加了Routes [“home”]并导航到localhost / home。 I couldn't use Routes[""] however because I'm using Razor pages and this caused an ambiguousActionException.
我不能使用Routes [“”]因为我使用的是Razor页面,这导致了一个模糊的ActionException。
I then realized I didn't need to use a controller at all with Razor Pages. 然后我意识到我不需要使用Razor Pages的控制器。 I could just access my data directly from Index.cshtml.cs
我可以直接从Index.cshtml.cs访问我的数据
public class IndexModel : PageModel
private readonly AmazonSettings _amazonSettings;
public string LoginUrl;
public IndexModel(IOptions<AmazonSettings> amazonSettings)
{
_amazonSettings = amazonSettings.Value;
}
With the following access in my Index.cshtml page: 通过我的Index.cshtml页面中的以下访问权限:
<a href=@Model.LoginUrl><h1>@Model.LoginUrl</h1></a>
It turns out that when debugging, the .Value returned by GetSection in the Startup code may be null, however it is populated by the time it reaches the IndexModel. 事实证明,在调试时,GetSection在启动代码中返回的.Value可能为null,但是它会在到达IndexModel时填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.