Hi @jp2code
In older ASP.NET WebForms, I could store and retrieve any data I needed in the
Web.config
file. Now in ASP.NET Core 7, I see how to get the database connection string using theappsettings.json
file, but it only appears to be accessible from Program.cs before the application starts.
As we all known, in the older WebForms application, the configuration setting is stored in the Web.config
file. To read the configuration setting, we need to use the System.Configuration
Assembly to read XML configuration files such as the Web.config file. And then, use the ConfigurationManager.GetSection()
method or ConfigurationManager.AppSettings
to get the configuration setting.
In asp.net core application, it supports many methods of configuration. In ASP.NET Core application, the configuration is stored in name-value pairs, and it can be read at runtime from various parts of the application. The application configuration data may come from
- File, such as JSON, XML, INI
- Environment variables
- Command Line arguments
- An in-memory collection
- Custom providers
Generally, the configuration setting is stored in the appsetting.json file, like this:
Then, to access the value, we need to use Microsoft.Extensions.Configuration
extension and the IConfiguration
interface (this interface provides the related method to get the configuration value).
In Program.cs file, the code like this, this is similar like you did in the WebForms appplication: to get the data via ConfigurationManager
.
Then, in the Controller, since the application configuration is used with dependency injection, we don't need to register it in the program.cs file or use the new
key word to create an IConfiguration
instance, we can directly use the constructor injection method to use it, and the DI container will help to create the instance.
More detail information about the configuration, see Configuration in ASP.NET Core.
I have an API that has assigned me a Client ID and a Client Secret.
Do you mean the client id and client secret is belong to personal? If that is the case, appsettings.json file might not a good choice. You can store the personal data into database or use Azure Key Vault. Generally, the appsettings.json file could use to store the public configuration value for the application, everyone can access it.
Besides, about the use of dependency injection, the official documentation already explains it clearly.
For example, we have the following Interface and Class:
public interface IDateTime
{
DateTime GetCurrentDate();
}
public class SystemDateTime : IDateTime
{
public DateTime GetCurrentDate ()
{
return DateTime.Now;
}
}
In the older WebForm application, to call the GetCurrentDate method, we have to use the new
key word to create the class instance, then call the GetCurrentDate method.
But in the Asp.net core. using dependency injection, we can do it as below:
register the service in the program.cs file: then the DI container will create the instance based on the service lifetime.
builder.Services.AddScoped<IDateTime, SystemDateTime>();
In the controller, inject the IDateTime, without using the new
key word, after that we can call the related method.
More information, see:
Overview of dependency injection
Dependency injection into controllers in ASP.NET Core
Getting Started with Dependency Injection in ASP.NET Core using C#
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion