0

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?

enter image description here

TylerH
  • 20,799
  • 66
  • 75
  • 101
Rashed Hossen
  • 81
  • 1
  • 9

1 Answers1

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