Oren Baldinger Oren Baldinger - 4 months ago 11
Ini Question

Environment variables file in C#?

I'm new to C#, coming from a PHP background I'm accustomed to storing things like database/smtp config in an environment file excluded from source control, so that the application can run locally and in multiple environments. Is there an equivalent convention in C#/ASP.NET? Maybe an .ini file somewhere? Web.config is a possible candidate, but there is dependency information in there as well (like a composer.json), so maybe not.

Answer

web.config is correct. You can add custom key/value pairs using the appSettings tag:

<appSettings>
    <add key="CustomKey" value="Custom Value" />
</appSettings>

and then accessing it through:

ConfigurationManager.AppSettings["key"]

ASP.NET Web Configuration Guidelines ConfigurationManager.AppSettings Property

Comments