Home > Net >  Is there a way to inject configuration settings(IConfiguration) in a view or cshtml file?
Is there a way to inject configuration settings(IConfiguration) in a view or cshtml file?

Time:04-06

I am trying to read the Appsettings.json file in a view or cshtml directly. Is this possible? In a class i can just pass into my constructor as IConfiguration and then use it to get sections of my config like _configuration.GetSection("Application:AppDomain").Value so i am trying to do the same to the views or cshtml directly. Is there a way?

CodePudding user response:

Yes, you can definitely do that by injecting the IConfiguration on your View. It will look like this:

@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
<h1>@Configuration.GetSection("Application:AppDomain").Value</h1>
  • Related