ASP.NET Core 2.0 read: Options in razor page

2019-02-24 10:19发布

问题:

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.

回答1:

you still need to add the below code in your startup.cs file

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

also you need to set the default value of RefreshTime in appsettings.json and then use the below code in.cshtml page

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

let me know if it works for you.



回答2:

The answer is here:

The @page Razor directive makes the file into an MVC action — which means that it can handle requests. @page must be the first Razor directive on a page.

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>


标签: c# asp.net razor