Accessing app.config in ASP.NET

北城以北 提交于 2019-12-20 01:08:52

问题


I am using app.config file to read data from it.. I am loading the app.config file as:

string app_path = HttpContext.Current.Server.MapPath("app.config");
xmlDoc.Load(app_path);

string image_path = ConfigurationManager.AppSettings["Test1"];

and i want to get the value of "Test1". But the value of test1 is comming "null".. how can i get the value of "test1" from app.config file.. i created the app.config file as:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="Test1" value="My value 1" />
    <add key="Test2" value="Another value 2" />
  </appSettings>
</configuration>

please help me out..


回答1:


In ASP.NET applications, the default configuration file is named web.config. This is a convention you should probably stick to, that allows you to easily use the ConfigurationManager to access configuration settings.

I suggest having a look at http://en.wikipedia.org/wiki/Web.config as a starting point to learn the ins and outs of basic .NET application configuration in the ASP.NET domain.

You can link configuration files together, by setting the file attribute of configuration sections you want to override: http://www.codeproject.com/KB/dotnet/appsettings_fileattribute.aspx




回答2:


Web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="Test1" value="My value 1" />
    <add key="Test2" value="Another value 2" />
  </appSettings>
</configuration>

Code:

string image_path = ConfigurationManager.AppSettings["Test1"];



回答3:


If you want to use ConfigurationManager.AppSettings inside a web application, you have to put your AppSettings section in web.config.



来源:https://stackoverflow.com/questions/5989736/accessing-app-config-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!