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

- Crud operation in asp.net core using entity framework core code first - May 22, 2020
- How to handle sessions in asp.net core with example - April 30, 2020
- How to upload multiple files in asp.net core - April 29, 2020
Wow great
thanks
Useful information
thanks