Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 678
user3693957 Avatar asked Feb 25 '18 22:02

user3693957


People also ask

How do I run Appsettings json at startup?

In order to add AppSettings. json file, right click on the Project in Solution Explorer. Then click Add, then New Item and then choose App Settings File option (shown below) and click Add button. Once the File is created, it will have a DefaultConnection, below that a new AppSettings entry is added.

Where can I find Appsettings json?

appsettings. json is one of the several ways, in which we can provide the configuration values to ASP.NET core application. You will find this file in the root folder of our project. We can also create environment-specific files like appsettings.

How does Appsettings json work?

The appsettings. json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information.


Video Answer


3 Answers

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"]
like image 116
René Avatar answered Nov 08 '22 07:11

René


If you use the options pattern you can inject them into your view like this:

@using Microsoft.Extensions.Options
@inject IOptions<ApplicationInsightsOptions> 
ApplicationInsightsOptionsAccessor
@
{
   var instrumentationKey = 
        ApplicationInsightsOptionsAccessor.Value.InstrumentationKey;
}

Options pattern in ASP.NET Core

like image 32
toddmillernyc Avatar answered Nov 08 '22 07:11

toddmillernyc


Using ActionFilters you can interrupt the request and add the configuration variables maybe to the ViewBag 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

{
    "MyConfig": {
        "MyValue": "abc-def"
    }
}

In the code MyConfig.cs would be:

public class MyConfig
{
    public string MyValue{ get; set; }
}

First create a very simple ActionFilter which derives from IAsyncActionFilter as following :

public class SampleActionFilter : IAsyncActionFilter
{
    private MyConfig _options;
    public SampleActionFilter(IConfiguration configuration)
    {

        _options = new MyConfig();
        configuration.Bind(_options);
    }

    public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
        ((Microsoft.AspNetCore.Mvc.Controller)context.Controller).ViewBag.MyConfig = _options;
        await next();
    }
}

Later in the Startup.ConfigureServices method change services.AddMvc to the following:

public void ConfigureServices(IServiceCollection services)
{

    //..........

    services.AddMvc(options=>
    {
        options.Filters.Add(new SampleActionFilter(
            Configuration.GetSection("MyConfig")
        ));
    });

    //..........

}

To access the values just simply in the _Layout.cshtml or other view you can type:

@ViewBag.MyConfig.MyValue
like image 25
Amer Sawan Avatar answered Nov 08 '22 08:11

Amer Sawan