Getting Value From Appsettings.json In .NET Core - Telerik
Maybe your like
Learn how to fetch properties or values from appsettings.json in .NET Core. We’ll cover it using both IConfiguration and Options Pattern.
Introduction
In this article, we are going to learn how to fetch properties or values from appsettings.json in .NET Core. We'll cover the following topics:
- Getting values from appsettings.json
- Using IConfiguration
- Using Options Pattern
- Conclusion
Getting Values from appsettings.json
There are one or more ways available to fetch values from appsettings.json file in .NET Core. Following are the two ways we are going to learn:
- Using IConfiguration
- Using Options Pattern
Using IConfiguration
The IConfiguration is available in the dependency injection (DI) container, so you can directly access JSON properties by simply injecting IConfiguration in the constructor of a controller or class. It represents a set of key/value application configuration properties.
IConfiguration is used as follows:
- Create a simple web or web API project in .NET Core
- Create a controller or use default one
- Define one readonly property of type IConfiguration and inject in your controller and access JSON properties as follows:
Using Options Pattern
The first option we discussed is easy to use, but if you want to access multiple values then you need to provide the keys and it shows hard coded values in controllers or classes everywhere. To access multiple values or hierarchy/structured data from appsettings.json, Options Pattern is a good option. It uses classes to represent the group of hierarchy/structured settings.
Options Pattern is used as follows:
- Create a simple web or web API project in .NET Core
- Create a controller or use default one
- Create a class to represent the hierarchy/structured settings (replica of appsettings.json file):
- Register in the configuration instance in startup.cs class of ConfigureServices method as follows:
- Define one readonly property of type IOptions<T> and inject in your controller and access JSON properties as follows:
You can also download this example from here.
Conclusion
In this article, we discussed how to access settings from appsettings.json in .NET Core using simple examples. If you have any suggestions or queries regarding this article, please contact me.
“Learn It, Share it.”
Tag » Appsettings.json Guid
-
Dictionary
Mapping From Appsettings.json In C# - Stack ... -
Strongly Typed Configuration Settings In ASP.NET Core Part II
-
Reading Lists From Appsettings.json - BitScry
-
Safe Storage Of App Secrets In Development In ASP.NET Core
-
ASP.NET Core: Step By Step Guide To Access Appsettings.json In ...
-
Unable To Bind Dictionary With Int Key Type From Appsettings.json To ...
-
Dell Trusted Device Installation And Administrator Guide V4.5
-
How To Use ASP.NET Core IOptions Pattern - Referbruv
-
Custom Configuration Section (Array) In Appsettings.json - Anycodings
-
Settings | Documentation Center | ABP.IO
-
Tutorial - ASP.NET Core First Query With Couchbase SDK 3
-
Using User Secrets Configuration In .NET -.NET Core Tutorials
-
A Step By Step Guide For ASP.NET Core Configuration
-
Configure .NET Core Pull Relay Server, PS V2.0 - VMware Docs