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

Reading Values From Appsettings.json In ASP.NET Core

1094

Subscribe Youtube & Download free

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


  • |
  • Reading Values From Appsettingsjson In ASPNET Core 31 and 60 , appsettingsjson , ASPNET Core , Reading Values From appsettingsjson

Comments

Follow Us On Social Media - Like Us On Facebook

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 New Subscribers!

Subscribe and Click on Verify and Download for download link

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

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading

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 © 2023 by Code2night. All Rights Reserved

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