ASP.NET Core 2.0 read: Options in razor page

前端 未结 2 1444
南方客
南方客 2021-01-19 04:55

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


        
相关标签:
2条回答
  • 2021-01-19 05:24

    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>
    
    0 讨论(0)
  • 2021-01-19 05:25

    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.

    0 讨论(0)
提交回复
热议问题