如何将 appsettings.json 配置注入到 razor 页面 C# 代码块中?

Mun*_*kin 5 c# razor asp.net-core

我正在关注以下官方文档:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/ ?view=aspnetcore-6.0#access-configuration-in-razor-pages

这种实现@Configuration["myKey"]非常适合不在@{}括号之间的代码,但是当我有一个代码块时,它根本不起作用。

据我所知,该文档没有提供代码示例......

我该如何解决这个问题?

代码当然位于 Razor 页面 (.cshtml) 中。

我尝试删除@并放入不带 的相同代码@,但随后它给出了上下文错误......

PS 如果重要的话,有问题的代码是 POCO。

我使用 PPS@inject IConfiguration Configuration在 Razor 页面顶部导入配置。

我的有问题的代码:

var website = new WebSite()
        {
    private string altName = Configuration["FrameworkData:PageTitle"] +" - " +Configuration["FrameworkData:Slogan"];
    AlternateName = altName,
....
Run Code Online (Sandbox Code Playgroud)

我已经尝试在配置规范之前指定 IConfiguration 类型,但没有任何效果。

更新

我的起始代码包含有问题的部分:

@using Microsoft.AspNetCore.Http;
@using Schema.NET;
@model projectname2.Areas.MyFeature.Pages.Shared._LayoutModel
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@{  
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name='description' content='@ViewData["Description"]'>
    <title>@ViewData["Title"] - @Configuration["FrameworkData:PageTitle"]</title>
    @{
        var website = new WebSite()
        {
            private string altName = "";
    string altName = $"{Configuration["FrameworkData:PageTitle"]} - {Configuration["FrameworkData:Slogan"]}";
    AlternateName = altName,
    Name = Configuration["FrameworkData:PageTitle"],
    Url = new Uri("https://example.com")
};
        var jsonLd = website.ToString();
        var address = new PostalAddress()
        {
        StreetAddress = "Example street 10",
        AddressLocality = "NY",
        PostalCode = "11111",
        AddressCountry = "US"
        };
        var geo = new GeoCoordinates()
        {
            Latitude = 0.3947623,
            Longitude = 0.8723408
        };
        var localbusiness = new LocalBusiness()
            {
                Name = "Project name",
                Image = new Uri("https://example.com/graphics/logo.svg"),
                Url = new Uri("https://example.com"),
                Telephone = "1234 1243567",
                Address = address,
                Geo = geo,
                SameAs = new Uri("https://example.com"),
                OpeningHours = "Mo-Fr 08:00-17:00",
            };
        
        var jsonLdlb = localbusiness.ToString();
        var organization = new Organization()
            {
                AreaServed = "US",
                ContactPoint = new ContactPoint()
                {
                    AvailableLanguage = "English",
                    ContactOption = ContactPointOption.TollFree,
                    ContactType = "customer service",
                    Telephone = "1234 1243567"
                },
                Url = new Uri("https://example.com"),
                Logo = new Uri("https://example.com/graphics/logo.svg"),
            };
            var jsonLdorg = organization.ToString();
    }
<script type="application/ld+json">
    @Html.Raw(jsonLd)
</script>
<script type="application/ld+json">
    @Html.Raw(jsonLdlb)
</script>
<script type="application/ld+json">
    @Html.Raw(jsonLdorg)
</script>
Run Code Online (Sandbox Code Playgroud)

小智 2

@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@{
   string myValue = Configuration["FrameworkData:PageTitle"];
   // Do your things
}
Run Code Online (Sandbox Code Playgroud)

请参阅微软文档