Configuration.GetSection返回空值 [英] Configuration.GetSection returns null value

查看:656
本文介绍了Configuration.GetSection返回空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让我的Configuration.GetSection返回.Value中的数据.我想我已经实施了问题的所有建议,但仍然无法解决工作.

I cannot get my Configuration.GetSection to return data in .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"
    },
}

启动:

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类:

AmazonSettings Class:

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:

I'm attempting to access AmazonSettings.Value via IOptions:

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:

推荐答案

我的问题是,我在HomeController中的代码从未遭到攻击.

My problem was that my code in HomeController was never getting hit.

如果我在控制器上方添加Routes ["home"]并导航到localhost/home,则可以到达那里,并填充.Value.我无法使用Routes ["],因为我使用的是Razor页面,这导致了ambiguousActionException.

I could get there, and .Value was populated, if I added Routes["home"] above my controller and navigated to localhost/home. I couldn't use Routes[""] however because I'm using Razor pages and this caused an ambiguousActionException.

然后我意识到Razor Pages根本不需要使用控制器.我可以直接从Index.cshtml.cs

I then realized I didn't need to use a controller at all with Razor Pages. I could just access my data directly from Index.cshtml.cs

public class IndexModel : PageModel
    private readonly AmazonSettings _amazonSettings;
    public string LoginUrl;

    public IndexModel(IOptions<AmazonSettings> amazonSettings)
    {
        _amazonSettings = amazonSettings.Value;
    }

在我的Index.cshtml页面中具有以下访问权限:

With the following access in my Index.cshtml page:

<a href=@Model.LoginUrl><h1>@Model.LoginUrl</h1></a>

事实证明,在调试时,GetSection在启动代码中返回的.Value可能为null,但是会在到达IndexModel时填充它.

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.

这篇关于Configuration.GetSection返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆