Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
790 views
in Technique[技术] by (71.8m points)

razor - How to read appsettings.json in my _layout.chtml

I cannot seem to figure out how to read values from the appsettings.json in my _Layout.chtml file.

Is it not just available, something like this? @Configuration["ApplicationInsights:InstrumentationKey"]

I created a new MVC project using razor pages.

fyi, i'm an mvc newbee - code samples help a lot.

question from:https://stackoverflow.com/questions/48979294/how-to-read-appsettings-json-in-my-layout-chtml

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...