Search code examples
c#asp.net-core-mvc

Show appsettings.json value in cshtml file in .NET Core


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 ?


Solution

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