ASP.NET Core 2.0阅读:剃须刀页面中的选项 [英] ASP.NET Core 2.0 read: Options in razor page
问题描述
按照本指南,我正在尝试在ASP.NET Core 2.0页面上显示设置值.
Following this guide, I'm trying to show on an ASP.NET Core 2.0 page the settings values.
在startup.cs中,我添加了一些服务:
In startup.cs I added some services:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddOptions();
services.Configure<MyOptions>(Configuration);
此处定义了MyOptions:
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; }
}
}
从startup.cs尝试,我可以访问RefreshTime设置.现在,我想直接在cshtml中执行此操作:
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>
该页面无法加载,但在VisualStudio的输出控制台或浏览器之一中都看不到任何错误.
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.
此外,即使我只是将第一个@using指令放在此处,也删除了所有其他选项,也无法加载页面.
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?
正如@Niladri指出的,我混合了MVC和Razor页面(我将在此主题上打开一个单独的问题).
As @Niladri pointed out I mixed MVC and Razor pages (I will open a separate question on this topic).
现在,我删除了控制器类,因为剃须刀页面不需要.无论如何,主要的问题是,如果我在任何页面上使用Microsoft.Extensions.Options 添加 @,这将不允许页面加载.
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.
推荐答案
答案是此处:
@page Razor指令使文件成为MVC操作-这意味着它可以处理请求. @page必须是页面上的第一个Razor指令.
因此,解决方案非常简单,只需对指令重新排序:
Hence the solution is extremely simple, just reorder the directives:
@page
@using Microsoft.Extensions.Options
@inject IOptions<MyOptions> Options
@model AboutModel
@{
ViewData["Title"] = "About";
}
<
<h3>@Model.Message</h3>
<br />
<div>
<h3>Options</h3>
<p><b>Refresh time</b> @Options.Value.SubOpt.RefreshTime</p>
</div>
这篇关于ASP.NET Core 2.0阅读:剃须刀页面中的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!