I am calling some API from ASP.NET Core MVC application and I hard coded the API inside the controller along with the authorization key. How can I move this API list and authorization key in the appsetting.json file, so I can change the configuration file later for production as well?
Asked
Active
Viewed 2,377 times
0
-
Please detail the version of .net core you are using. – Travis Acton Oct 02 '19 at 15:51
-
Targeted version:
netcoreapp3.0 – Rashed Hossen Oct 02 '19 at 16:23
1 Answers
3
In your appsettings.json file, add api url's like below,
"AuthKey": "your_authorization_key",
"SomeApi": {
"Clinical": "https://tsrvcis/...?format=json",
"Demographic": "https://tsrvcis/...?format=json"
}
And then in the code access it in your HomeController like,
using Microsoft.Extensions.Configuration;
public class HomeController : Controller
{
private readonly IConfiguration _configuration;
public HomeController(IConfiguration configuration)
{
_configuration = configuration;
}
public JsonResult GetAPIData(string param)
{
var authKey = _configuration.GetValue<string>("AuthKey");
var clinicalUrl = _configuration.GetSection("SomeApi:Clinical").Value;
}
}

fingers10
- 6,675
- 10
- 49
- 87