As.Net core tutorials

Updated : Apr 28, 2020 in Asp.net core

How to read values from AppSettings.json File In Asp.net core

This session we will learn how to read the values from AppSettings.json File In Asp.net core

In asp.net core appsettings.json is important it can contain the Environment variables, azure keys, connection string…and in asp.net core doesn’t have web.config file this lesson we will show you how to add and read application setting using appseeting.json

Create ASP.NET Core MVC Project: Go to Visual Studio, create a new ASP.NET Core Web Application project

Select Empty Template and Click Ok button to Finish

Add Configurations: Open Startup.cs file and add new configurations as below:

namespace ControllerToView
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Demo}/{action=Index}/{id?}");
            });

        }
    }
}

in asp.net core Startup.cs file we will use for register the configuration services, Application services, using the app through dependency injection, and creating the requests of the app process pipelines and it is an entry point of the application.

Add AppSettings File: Select Project and right click to select Add\New Item 

Select App Settings File item and click Add button to Finish and add the following snippet code In appsettings.json file and new configurations as below:

{
  "Message": "Hello World",
  "MyConfigs": {
    "Config1": "Value of Config file 1",
    "Config2": "Value of Config file 2",
    "Config3": "Value of Config file 3"
  },
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}

Create Controller: Create a new folder named Controllers. In this folder, create a new controller named DemoController.cs as below:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace ControllerToView.Controllers
{
    public class DemoController:Controller
    {
        public IActionResult Index()
        {
            var builder = new ConfigurationBuilder()
                                    .SetBasePath(Directory.GetCurrentDirectory())
                                    .AddJsonFile("appsettings.json");
            var configuration = builder.Build();
            ViewBag.resultA = configuration["Message"];
            ViewBag.resultB = configuration["MyConfigs:Config1"];
            ViewBag.resultC = configuration["MyConfigs:Config2"];
            ViewBag.resultD = configuration["MyConfigs:Config3"];
            ViewBag.resultE = configuration["Logging:Debug:LogLevel:Default"];
            return View();
        }
    }
}

Create View: Create a new folder name like  Views. In this folder, create a new folder named Demo. Create a new view named Index.cshtml as below:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>

    Result 1: @ViewBag.resultA
    <br />
    Result 2: @ViewBag.resultB
    <br />
    Result 3: @ViewBag.resultC
    <br />
    Result 4: @ViewBag.resultD
    <br />
    Result 5: @ViewBag.resultE

</body>
</html>

See the following given Structure of ASP.NET MVC Core Project

Run Application Your Application: you will Access Index action in Demo controller with the following URL: http://localhost:48982/Demo/Index see the given below output

SandeepMVN
Like
Like Love Haha Wow Sad Angry
2

Subscribe
Notify of
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
shivam
April 29, 2020 5:32 am

Wow great

DuttaluruVijayakumar
Admin
May 15, 2020 1:08 pm
Reply to  shivam

thanks

chaitrav
April 29, 2020 5:35 am

Useful information

DuttaluruVijayakumar
Admin
May 15, 2020 1:08 pm
Reply to  chaitrav

thanks

4
0
Would love your thoughts, please comment.x
()
x