web.config file access in another project
-
Hello All, Our main 2012 VS solution has 16 projects. One of the project is a web site, which a web.config file defined. I want to access this website's web.config file's applicationSettings value in another project. I added the System.Configuration to this class. And using below trying to access the value of the settings. Since this setting is defined as a string, converting it to an int. The value for the setting QuesId is 2 in the web.config file. The code complies with no errors, but when i execute and browse the code, the value is null. any ideas??
Int32.Parse(ConfigurationManager.AppSettings["QuesId"]);
-
Hello All, Our main 2012 VS solution has 16 projects. One of the project is a web site, which a web.config file defined. I want to access this website's web.config file's applicationSettings value in another project. I added the System.Configuration to this class. And using below trying to access the value of the settings. Since this setting is defined as a string, converting it to an int. The value for the setting QuesId is 2 in the web.config file. The code complies with no errors, but when i execute and browse the code, the value is null. any ideas??
Int32.Parse(ConfigurationManager.AppSettings["QuesId"]);
How are you executing the other project? If it's a class library called from within the web site, the code you've posted should work. If it's a stand-alone application, it will need its own configuration file.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
How are you executing the other project? If it's a class library called from within the web site, the code you've posted should work. If it's a stand-alone application, it will need its own configuration file.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Richard Deeming wrote:
If it's a class library called from within the web site, the code you've posted should work. If it's a stand-alone application, it will need its own configuration file.
Methinks that he has a config file for the library {1}, and one for the web app{2}. And now he has troubles accessing {1} when the library method is executed from the web app since the library will try to access {2}.
Clean-up crew needed, grammar spill... - Nagy Vilmos
-
Richard Deeming wrote:
If it's a class library called from within the web site, the code you've posted should work. If it's a stand-alone application, it will need its own configuration file.
Methinks that he has a config file for the library {1}, and one for the web app{2}. And now he has troubles accessing {1} when the library method is executed from the web app since the library will try to access {2}.
Clean-up crew needed, grammar spill... - Nagy Vilmos
In the web.config file, if I use the then I am able to access this setting in my other C# project using, with no issues:
Int32.Parse(ConfigurationManager.AppSettings["QId"]);
But if it is defined in the part of web.config, when I try to access it in my other C# project:
Int32.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["QId"])
a null value is returned. Is my syntax incorrect?
-
In the web.config file, if I use the then I am able to access this setting in my other C# project using, with no issues:
Int32.Parse(ConfigurationManager.AppSettings["QId"]);
But if it is defined in the part of web.config, when I try to access it in my other C# project:
Int32.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["QId"])
a null value is returned. Is my syntax incorrect?
No. The problem most likely is that
Int32.Parse(ConfigurationManager.AppSettings["QId"]);
is accessing the app.config file, while
Int32.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["QId"])
is accessing the web.config. Furthermore, if you have two configuration files, only the file of the project you build as Startup project is read by either
ConfigurationManager.AppSettings
orWebConfigurationManager.AppSettings
.Clean-up crew needed, grammar spill... - Nagy Vilmos
-
No. The problem most likely is that
Int32.Parse(ConfigurationManager.AppSettings["QId"]);
is accessing the app.config file, while
Int32.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["QId"])
is accessing the web.config. Furthermore, if you have two configuration files, only the file of the project you build as Startup project is read by either
ConfigurationManager.AppSettings
orWebConfigurationManager.AppSettings
.Clean-up crew needed, grammar spill... - Nagy Vilmos
-
Actually both settings are defined in the web.config file, but under different sections. I am not using the app.config file in my other project.
Can you try to use ConfigurationManager.AppSettings instead of WebSettings?
Clean-up crew needed, grammar spill... - Nagy Vilmos
-
Can you try to use ConfigurationManager.AppSettings instead of WebSettings?
Clean-up crew needed, grammar spill... - Nagy Vilmos
-
Can you post the config file?
Clean-up crew needed, grammar spill... - Nagy Vilmos