如何在剃刀页面 C# 代码块中注入 appsettings.json 配置?
How do you inject your appsettings.json configuration in a razor page C# code block?
@Configuration["myKey"]
的这种实现非常适合不在 @{}
括号之间的代码,但是当我有一个代码块时它根本不起作用。
据我所知,文档没有提供任何代码示例...
我该如何解决这个问题?
代码当然在 Razor 页面 (.cshtml) 中。
我尝试删除 @
并在没有 @
的情况下输入相同的代码,但随后出现上下文错误...
P.S。如果重要的话,相关代码是 POCO。
P.P.S。我使用 @inject IConfiguration Configuration
在 Razor 页面顶部导入配置。
我的问题代码:
var website = new WebSite()
{
private string altName = Configuration["FrameworkData:PageTitle"] +" - " +Configuration["FrameworkData:Slogan"];
AlternateName = altName,
....
我已经尝试在配置规范之前指定 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>
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@{
string myValue = Configuration["FrameworkData:PageTitle"];
// Do your things
}
我不得不在另一个代码块中定义有问题的代码块之外的变量,如下所示:
@{
var pageTitle = Configuration["FrameworkData:PageTitle"];
}
我必须在初始化 WebSite()
实例之前定义 altName
:
var altName = $"{pageTitle} - {slogan}";
var website = new WebSite()
然后我就可以通过变量名引用变量了。
案件结案。
@Configuration["myKey"]
的这种实现非常适合不在 @{}
括号之间的代码,但是当我有一个代码块时它根本不起作用。
据我所知,文档没有提供任何代码示例...
我该如何解决这个问题?
代码当然在 Razor 页面 (.cshtml) 中。
我尝试删除 @
并在没有 @
的情况下输入相同的代码,但随后出现上下文错误...
P.S。如果重要的话,相关代码是 POCO。
P.P.S。我使用 @inject IConfiguration Configuration
在 Razor 页面顶部导入配置。
我的问题代码:
var website = new WebSite()
{
private string altName = Configuration["FrameworkData:PageTitle"] +" - " +Configuration["FrameworkData:Slogan"];
AlternateName = altName,
....
我已经尝试在配置规范之前指定 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>
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@{
string myValue = Configuration["FrameworkData:PageTitle"];
// Do your things
}
我不得不在另一个代码块中定义有问题的代码块之外的变量,如下所示:
@{
var pageTitle = Configuration["FrameworkData:PageTitle"];
}
我必须在初始化 WebSite()
实例之前定义 altName
:
var altName = $"{pageTitle} - {slogan}";
var website = new WebSite()
然后我就可以通过变量名引用变量了。
案件结案。