Code2night
  • Home
  • Blogs
  • Tutorial
  • Post Blog
  • Tools
    • Json Beautifier
    • Html Beautifier
  • Members
    • Register
    • Login
  1. Home
  2. Blogpost
10 Sep
2022

Reading Values From Appsettings.json In ASP.NET Core

by Shubham Batra

780

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

Best Sellers

product 1

Hand Hug Bracelet For Women Men Cuff Bangle Adjustable Lover Couple Bracelets

Can be given as a gift to your family, relatives, or friends

Buy $15.99
product 1

Teddy bear hug bear plush toy bear cub

Can be given as a gift to your family, relatives, or friends


Buy $49.99

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
Thank you for Downloading....!

Subscribe for more tutorials

Support our team

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