locked
Basic config files RRS feed

  • Question

  • User-1458727574 posted

    I'm not looking for anything complex here but, every time I read another page about this, it starts wading into stuff that is way too much for what I need. My web service creating in ASP.NET Core 2.1 needs to have a config file. In simpler times, I would have a config.xml or something. It will contain info such as:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
       <appSettings>
          <add key="UserId" value="user1" />
          <add key="UserPassword" value="Password123" />
          <add key="InterfaceVersion" value="1.0" />
          <add key="InterfaceName" value="WebApi" />
          <add key="TargetServer" value="localhost" />
          <add key="UserHTTPS" value="N" />
          <add key="LogFilePath" value="./ApiLogs/" />
       </appSettings>
    </configuration>

    When the web api is called, it needs to read the values from this config file. Don't worry about the password being in plain text. I have 2 controllers in my api that pick up HTTPPost data. During that call, the config needs to be read to pick up the info in it. That cannot be complicated can it? Can someone point me in the right direction or give me an example?

    Thursday, June 28, 2018 9:06 AM

Answers

  • User475983607 posted

    There is no getting around using the standard ASP Core configuration convention.  That means reading and understanding the docs.  

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1&tabs=basicconfiguration

    Configuration is stored in the appsetting.json file.  The configuration object is injected using DI. 

    Create a simple project to play with configuration.  Once you are able to read from configuration files, then add environments to the mix.  Create a  appsetting.development.json, appsetting.staging.json, and appsetting.production.json.  The environment is driven by the ASPNETCORE_ENVIRONMENT variable which you can find in the project properties under debug.  Change development to staging to see what happens.

    Next, move all of the sensitive configuration to "User Secrets" on your development machine and to environment variables on the web servers.  Keep the other stuff in appsetting.XXX.json.  For example, my staging server has an environment variable ASPNETCORE_ENVIRONMENT = Staging.  I do this once and any successive deployments invoke Staging settings.

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 28, 2018 11:07 AM

All replies

  • User475983607 posted

    There is no getting around using the standard ASP Core configuration convention.  That means reading and understanding the docs.  

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1&tabs=basicconfiguration

    Configuration is stored in the appsetting.json file.  The configuration object is injected using DI. 

    Create a simple project to play with configuration.  Once you are able to read from configuration files, then add environments to the mix.  Create a  appsetting.development.json, appsetting.staging.json, and appsetting.production.json.  The environment is driven by the ASPNETCORE_ENVIRONMENT variable which you can find in the project properties under debug.  Change development to staging to see what happens.

    Next, move all of the sensitive configuration to "User Secrets" on your development machine and to environment variables on the web servers.  Keep the other stuff in appsetting.XXX.json.  For example, my staging server has an environment variable ASPNETCORE_ENVIRONMENT = Staging.  I do this once and any successive deployments invoke Staging settings.

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 28, 2018 11:07 AM
  • User-1458727574 posted

    That works :)

    Thursday, June 28, 2018 12:23 PM