c# – ASP.NET Core 2.0读取:剃刀页面中的选项


this指南之后,我试图在ASP.NET Core 2.0页面上显示设置值.

在startup.cs中我添加了一些服务:

services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddOptions();
services.Configure<MyOptions>(Configuration);

这里定义了MyOptions:

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; }
    }
}

尝试从startup.cs我可以访问RefreshTime设置.
现在我想在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>

页面没有加载,但我没有看到VisualStudio的输出控制台或浏览器中的任何错误.

此外,即使我只是放在第一个@using指令,删除所有其他选项的东西,页面也不会加载.

然后我检查我安装了这个包,看起来是这样的:

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

还有其他我需要做的事情吗?

UPDATE

正如@Niladri所指出的那样,我混合了MVC和Razor页面(我将在这个主题上打开一个单独的问题).

现在我删除了控制器类,因为剃刀页面不需要.
无论如何,主要的问题是,如果我在任何页面上添加@using Microsoft.Extensions.Options,这将不允许页面加载.

但我看不到任何错误或例外.

最佳答案 您仍需要在startup.cs文件中添加以下代码

services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddOptions();
services.Configure<MyOptions>(Configuration); 

你还需要在appsettings.json中设置RefreshTime的默认值,然后使用下面的代码in.cshtml页面

@using Microsoft.Extensions.Options;
@using <enter the namespace for your MyOptions class here>;
@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.SubOpt.RefreshTime</p>
</div>

请让我知道这对你有没有用.

点赞