How to read appsettings.json in my _layout.chtml

user3693957 picture user3693957 · Feb 25, 2018 · Viewed 16.5k times · Source

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.

Answer

René picture René · May 8, 2018

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