Reading from Custom .config files RRS feed

  • Question

  • User-567018338 posted

    Hi All,

    I am building an application using MVC 4 which makes serveral web service calls to a third party application. The API has a couple params such as a Token which must always be passed. I want to store these variables in a configuration file so I created a file called API.config in the root directory of the project. However I'm not really sure how I can read from this configuration file in my classes that make the web service calls.

    I tried this but it would not compile,

    Configuration conf = WebConfigurationManager.OpenWebConfiguration("~/API.config");

    private string apiToken = ConfigurationManager.AppSettings["APIToken"];


    Error 1 A field initializer cannot reference the non-static field, method, or property

    I'm new to ASP.NET so I'm not even sure if this is the best approach to stroring globals in ASP.NET.

    Monday, November 18, 2013 8:18 PM


  • User-821857111 posted

    The recommended place for storing this kind if information is in an AppSettings section in the standard web.config file:

        <add key="token" value="abc123" />

    Then you can reference it like this:

    var token = ConfigurationManager.AppSettings["token"];

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 19, 2013 1:27 AM