如何在我的 _layout.chtml 中读取 appsettings.json [英] How to read appsettings.json in my _layout.chtml
本文介绍了如何在我的 _layout.chtml 中读取 appsettings.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我似乎无法弄清楚如何从 _Layout.chtml 文件中的 appsettings.json 读取值.
I cannot seem to figure out how to read values from the appsettings.json in my _Layout.chtml file.
它不只是可用,像这样吗?@Configuration["ApplicationInsights:InstrumentationKey"]
Is it not just available, something like this? @Configuration["ApplicationInsights:InstrumentationKey"]
我使用剃刀页面创建了一个新的 MVC 项目.
I created a new MVC project using razor pages.
仅供参考,我是 mvc 新手 - 代码示例很有帮助.
fyi, i'm an mvc newbee - code samples help a lot.
推荐答案
在 .net core mvc 中,您可以通过在视图顶部添加以下两行来注入配置:
In .net core mvc you can inject the configuration by adding the following two lines at the top of your view:
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
然后您可以像这样访问该值:
You can then access the value like this:
@Configuration.GetSection("ApplicationInsights")["InstrumentationKey"]
这篇关于如何在我的 _layout.chtml 中读取 appsettings.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文