简体   繁体   English

Configuration.GetSection返回null值

[英]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.

相关问题 Configuration.GetSection 总是返回 Value 属性 null - Configuration.GetSection always returns Value property null Configuration.GetSection 从 appsetting.json 获取值但 Configuration.GetSection.Bind 总是返回 null - Configuration.GetSection gets value from appsetting.json but Configuration.GetSection.Bind always returns null configuration.getValue 或 configuration.getsection 总是返回 null - configuration.getValue or configuration.getsection always returns null GetService 和 Configuration.GetSection 从 jsonsettings 返回 null - GetService and Configuration.GetSection from jsonsettings returns null Configuration.GetSection(“的connectionStringName”)。获取 <?> 总是为空 - Configuration.GetSection(“ConnectionStringName”).Get<?> always null 配置 GetSection 返回对象部分的空值 - Configuration GetSection returns null value for object sections Configuration.GetSection 返回文件中不存在的 ConfigurationSection - Configuration.GetSection returns a ConfigurationSection that doesn't exists in the file 单元测试中的 HostBuilder:HostContext.Configuration.GetSection 返回值 null - HostBuilder in Unit Test: HostContext.Configuration.GetSection returns value null .Net Core Configuration.GetSection()。获取&lt;&gt;()不绑定 - .Net Core Configuration.GetSection().Get<>() not binding 如何使用 FakeItEasy 语法模拟 configuration.GetSection? - How to mock configuration.GetSection with FakeItEasy syntax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM