Show appsettings.json value in cshtml file in .NET Core
Asked Answered
S

3

6

I need to show value from the appsettings.json in a .cshtml file in ASP.NET Core 3.1 as we do in ASP.NET MVC like below:

@(System.Configuration.ConfigurationManager.AppSettings["SessionExpire"])

Can you help how to implement this in ASP.NET Core 3.1 MVC ?

Siphon answered 16/2, 2022 at 16:45 Comment(3)
Have you looked into the documentation about Configuration in ASP.NET Core? Is there part of the documentation you need help understanding?Daff
It depends on many factors. If you need some help you have to show the code you have done so far. the view where are you going to display and a controller with action where you are creating the view model.Deming
Be careful. The appsettings.json usually contains sensitive information, which should not be printed in a webpage or api response. It's a really bad idea...Antimalarial
W
22

Here is a demo to show value from appsettings.json in view:

appsettings.json:

{
  ...
  "Test": {
    "TestValue": "testValue"

  }
}

view:

@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@Configuration.GetSection("Test")["TestValue"]
Weber answered 17/2, 2022 at 2:13 Comment(1)
I tried this with my appsetting value defined in azure app service ApplicationInsights:ConnectionString and trying to refer that in my layout.cshtml like cfg: { connectionString: @Configuration.GetSection("ApplicationInsights")["ConnectionString"] } But this does not work and when I replace this with hardcoded value it does. Any suggestions?Kef
L
7

Yiyi You's answer is appropriate for when your value is under a section. But if your value is directly under the root of appsettings.json, for example:

{
    ...
    "MyValueAtRoot" : "value"
}

Then you need to call it from your view using:

@Configuration.GetValue(typeof(string), "MyValueAtRoot")

An additional interesting fact: The GetValue method has a generic overload method, but the returned value of such methods can't be rendered directly as html because the generic type parameter gets confused with plain html.

If you really want to use that method, you would first have to store the returned value in a variable, inside a razor code block, like this:

@{ var myValue = Configuration.GetValue<string>("MyValueAtRoot"); }

Then later render the variable:

<p>@myValue</p>

Note: For both of my examples, you also need the call the @using and @inject directives first, as described in the accepted answer.

Lenrow answered 2/5, 2023 at 2:23 Comment(0)
S
0

This works in a .razor page but was not working in .cshtml page since IConfiguration could not be found in .cshtml page

Syzygy answered 5/7 at 11:57 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Tamishatamma

© 2022 - 2024 — McMap. All rights reserved.