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

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,815 questions
0 comments No comments
{count} votes

Accepted answer
  1. 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"]);
            }
        }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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