I did not realize that: 'have a web.config in a separate class library and' was reading the web.config app setting from different web application.
I am using VS2010 target framework 3.5
I don't know what is wrong here but I am getting null
when I try to get ConfigurationManager.AppSettings["StoreId"];
private string _storeid = GetStoreId; public static string GetStoreId { get { return ConfigurationManager.AppSettings["StoreId"]; } }
web.config:
<appSettings> <add key="StoreId" value="123" /> </appSettings>
it is a .net builtin mechanism to define some settings before the application starts, without recompiling. see msdn configurationmanager.
ConfigurationManager was added to support ASP.NET Core's new WebApplication model, used for simplifying the ASP.NET Core startup code.
Why appsettings keys (in web. config) are not case sensitive ? The default comparer is a CaseInsensitiveComparer that uses the conventions of the invariant culture; that is, key comparisons are case-insensitive by default.
If you are UNIT TESTING you need A COPY of the APP.CONFIG inside the UNIT TEST PROJECT
The usual cause for this is due to context.
When you have a solution with two projects, if the App/Web.Config
is in the main project it wont work if the context of the running application is the second project such as a library, unit test, etc.
To read values from the config in other projects (with System.Configuration
) you'll need to move/copy the config file to the project with the running context. Unfortunately duplicating files defeats the tenants of good programming; OOP, Source Code Management, SOLID, etc.
A nifty solution is adding config file shortcuts in other projects so you only update one file:
It would be nice to divide the contents of config
files across project's. Elegantly, like Sharing Assembly Files as per answer #2: https://stackoverflow.com/a/15319582/495455 but alas it's by context
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