Configuration.GetSection returns 空值
Configuration.GetSection returns null value
我无法在 .Value
中获取 Configuration.GetSection
到 return 的数据。我想我已经实施了 中的所有建议,但仍然无法正常工作。
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();
}
配置服务:
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<AmazonSettings>(Configuration.GetSection("AmazonSettings"));
services.AddMvc()
亚马逊设置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:
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;
}
当我调试时,值为空:
我的问题是我在 HomeController 中的代码从未受到攻击。
如果我在我的控制器上方添加 Routes["home"] 并导航到 localhost/home,我可以到达那里,并且填充了 .Value。但是我无法使用 Routes[""],因为我使用的是 Razor 页面,这导致了 ambiguousActionException。
然后我意识到我根本不需要使用 Razor Pages 的控制器。我可以直接从 Index.cshtml.cs
访问我的数据
public class IndexModel : PageModel
private readonly AmazonSettings _amazonSettings;
public string LoginUrl;
public IndexModel(IOptions<AmazonSettings> amazonSettings)
{
_amazonSettings = amazonSettings.Value;
}
在我的 Index.cshtml 页面中具有以下访问权限:
<a href=@Model.LoginUrl><h1>@Model.LoginUrl</h1></a>
事实证明,在调试时,Startup 代码中 GetSection 返回的 .Value 可能为 null,但是它会在到达 IndexModel 时填充。
我无法在 .Value
中获取 Configuration.GetSection
到 return 的数据。我想我已经实施了
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();
}
配置服务:
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<AmazonSettings>(Configuration.GetSection("AmazonSettings"));
services.AddMvc()
亚马逊设置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:
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;
}
当我调试时,值为空:
我的问题是我在 HomeController 中的代码从未受到攻击。
如果我在我的控制器上方添加 Routes["home"] 并导航到 localhost/home,我可以到达那里,并且填充了 .Value。但是我无法使用 Routes[""],因为我使用的是 Razor 页面,这导致了 ambiguousActionException。
然后我意识到我根本不需要使用 Razor Pages 的控制器。我可以直接从 Index.cshtml.cs
访问我的数据public class IndexModel : PageModel
private readonly AmazonSettings _amazonSettings;
public string LoginUrl;
public IndexModel(IOptions<AmazonSettings> amazonSettings)
{
_amazonSettings = amazonSettings.Value;
}
在我的 Index.cshtml 页面中具有以下访问权限:
<a href=@Model.LoginUrl><h1>@Model.LoginUrl</h1></a>
事实证明,在调试时,Startup 代码中 GetSection 返回的 .Value 可能为 null,但是它会在到达 IndexModel 时填充。