Following this guide, I'm trying to show on an ASP.NET Core 2.0 page the settings values.
In startup.cs I added some services:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddOptions();
services.Configure<MyOptions>(Configuration);
where MyOptions is defined here:
namespace WebApplication1
{
public class OptionsController : Controller
{
private readonly SubOptions _SubOptions;
public OptionsController(IOptions<SubOptions> options)
{
_SubOptions = (SubOptions) options;
}
public IActionResult Index()
{
var RefreshTime = _SubOptions.RefreshTime;
return Content($"RefreshTime = {RefreshTime}");
}
}
public class MyOptions
{
public MyOptions()
{
SubOpt = new SubOptions();
}
public SubOptions SubOpt { get; set; }
}
public class SubOptions
{
public SubOptions()
{
RefreshTime = 5;
}
public int RefreshTime { get; set; }
}
}
Trying from startup.cs I can access to the RefreshTime setting. Now I want to do this directly in cshtml:
@using Microsoft.Extensions.Options
@model MyOptions
@inject IOptions<MyOptions> Options
@page
@model AboutModel
@{
ViewData["Title"] = "About";
}
<
<h3>@Model.Message</h3>
<br />
<div>
<h3>Options</h3>
<h4>Sub Options</h4>
<p><b>Refresh time</b> @Options.Value.SubOptions.RefreshTime</p>
</div>
The page doesn't load but I don't see any errors in the output console of VisualStudio nor in the one of the browser.
Furthermore the page doesn't load even if I just put there the first @using directive, removing all the other options stuff.
Then I checked I have that package installed and it seems so:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
Is there something other I need to do?
UPDATE
As @Niladri pointed out I mixed MVC and Razor pages (I will open a separate question on this topic).
Now I removed the controller class because is not needed for razor pages.
Anyway the main problem is that if I add @using Microsoft.Extensions.Options
on any page, this doesn't allow the page loading.
But I cannot see any errors or exceptions.