Share via

Read key from appsettings.json

Anonymous
2023-08-12T07:19:23.0433333+00:00

I have string BingMapsApiKey = "Au7sMtQzyQZRzuQ2krOIbZg8j2MGoHzzOJAmVym6vQjHq_BJ8a1YQGX3iCosFh8u"; in HomeController.cs I want to move it to appsettings.json.

Please suggest

https://github.com/KalyanAllam/LatLongAddress

Developer technologies | ASP.NET Core | Other
0 comments No comments

Answer accepted by question author

AgaveJoe 1,510 Reputation points
2023-08-12T10:54:33.0766667+00:00

I have string BingMapsApiKey in HomeController.cs I want to move it to appsettings.json. Please suggest

The following snippet was copied from the official documentation.

Configuration in ASP.NET Core

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "BingMapsApiKey": "123456789"
}

Controller

    public class ConfigController : Controller
    {
        private readonly IConfiguration Configuration;
        public ConfigController(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IActionResult Index()
        {
            return Ok(Configuration["BingMapsApiKey"]);
        }
    }

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.