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.
In .net core mvc you can inject the configuration by adding the following two lines at the top of your view:
You can then access the value like this:
Using
ActionFilters
you can interrupt the request and add the configuration variables maybe to theViewBag
so it becomes accessible from the views or from the_Layout.cshtml
File.For example, if the following configuration section is inside your
appsettings.json
In the code
MyConfig.cs
would be:First create a very simple ActionFilter which derives from
IAsyncActionFilter
as following :Later in the
Startup.ConfigureServices
method changeservices.AddMvc
to the following:To access the values just simply in the
_Layout.cshtml
or other view you can type:If you use the options pattern you can inject them into your view like this:
Options pattern in ASP.NET Core