Reading Values From Appsettings.json In ASP.NET Core | Code2night.com
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

Reading Values From Appsettings.json In ASP.NET Core

Date- Sep 10,2022

3292

Free Download
appsettingsjson ASPNET Core

I have added the following JSON object in the appsetting.json file.

 {
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",

  "credentials": {
    "UserName": "Shubham",
    "Password": "code2night",
    "Email": "code2night@gmail.com"
  }
}

We will create a model class in which we will define properties with the same name as we have defined in our appsettings.json, Create a class inside the model folder 

    public class CredSettingsModel
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }

    }

Go to Program.cs file and we will register our class by writing the below code.

 builder.Services.Configure<CredSettingsModel>(builder.Configuration.GetSection("credentials"));

Then we can inject it into our HomeController constructor using our IOptions instance.

using ConfigrationManager.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Diagnostics;

namespace ConfigrationManager.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        protected readonly IConfiguration _configuration;
        private readonly CredSettingsModel _credSettingsModel;
        public HomeController(ILogger<HomeController> logger, IOptions<CredSettingsModel> options)
        {
            _logger = logger;
            _credSettingsModel = options.Value;
        }

        public IActionResult Index()
        {
            string UserName = _credSettingsModel.UserName;
            string Password = _credSettingsModel.Password;
            string Email = _credSettingsModel.Email;
            return View();
        }

    }
}

Run the project and see the output it will show the username which we mentioned in the appsetting.json file


Comments

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1180
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts