I am trying to do this below, is there a way to do this or are web.configs only available at run-time, because I am getting a red line error saying must be compile time constant?
AppSettings:
<add key="MyString" value="TheValueOfTheString"/>
Code:
public const string MyString = ConfigurationManager.AppSettings["MyString"];
the problem is the use of const. const means the value is hard coded at design time.
const string MyString = "the text...";
an appsettings value is not known until runtime so it's not a constant value. instead you can use a static readonly value
static readonly MyString = ConfigurationManager.AppSettings["MyString"];
the difference is how the value is interpereted at compile time. when a constant is used the actual value is referenced, not the variable MyString
. a static readonly
value is compiled as the variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With