Okay, so, I have two major projects in a solution. One is a WebApp on NETFramework 4.7.2, which is the main app here, with the web pages, the controls, and so on. The other is a NETCore 3 app using the Worker Service template, basically, a windows service to run a couple of automated tasks.
For both apps, I set up a class library to consume from an external API, make the HTTP requests, and handle errors. The problem I'm getting is, the NetCore app won't read some keys from the web.config
file.
I learned that it is the best practice to keep some info like authentication keys on the web.config
and get them via ConfigurationManager, and it works fine when I consume the API via WebApp, but when I try to do the same on the NetCore side, the ConfigurationManager.AppSettings
always returns null. Here is the helper class that I use to get the key values in the library consuming the API:
using System.Configuration;
namespace ZoopAPI.Manager.Tools
{
public class ApiEndpoints
{
public static string getEndpoint(string endpoint)
{
var settings = ConfigurationManager.AppSettings;
string url = ConfigurationManager.AppSettings[endpoint];
return url;
}
public static string getConfigValue(string config, bool test)
{
string configValue = "";
if (test)
{
configValue = ConfigurationManager.AppSettings["Test" + config];
}
else
{
configValue = ConfigurationManager.AppSettings[config];
}
return configValue;
}
}
}
And the web.config
looks like this:
<appSettings>
<add key="ZoopApiV2" value="https://url/"/>
<add key="ZoopApiV1" value="https://anotherUrl/"/>
<add key="TestMarketplaceId" value="hexaDecimalCode"/>
<add key="TestDescontanetMaster" value="AnotherHexaDecimalCode"/>
<add key="TestAuthKey" value="Basic Auth Key"/>
<add key="MarketplaceId" value="Basic Auth Key"/>
<add key="DescontanetMaster" value="AnotherHexaDecimalCode"/>
<add key="AuthKey" value="Basic Auth Key"/>
<add key="webpages:Version" value="3.0.0.0"/>
<add key="webpages:Enabled" value="false"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
I'm not fully versed in the world of .NET but I know NETCore and NETFramework are different in some aspects so is there a compatibility issue? If possible, I'd like to avoid having one class in the library just to store the values as private strings as it seems unprofessional...? Many thanks in advance.
web.config
is no longer used by ASP.NET Core. The migration guide says:
ASP.NET Core no longer uses the Global.asax and web.config files that previous versions of ASP.NET utilized.
...
The web.config file has also been replaced in ASP.NET Core. Configuration itself can now be configured, as part of the application startup procedure described in Startup.cs. Configuration can still utilize XML files, but typically ASP.NET Core projects will place configuration values in a JSON-formatted file, such as appsettings.json. ASP.NET Core's configuration system can also easily access environment variables, which can provide a more secure and robust location for environment-specific values. This is especially true for secrets like connection strings and API keys that shouldn't be checked into source control. See Configuration to learn more about configuration in ASP.NET Core.