在 ASP.NET 中访问 app.config
我正在使用 app.config 文件从中读取数据。 我将 app.config 文件加载为:
string app_path = HttpContext.Current.Server.MapPath("app.config");
xmlDoc.Load(app_path);
string image_path = ConfigurationManager.AppSettings["Test1"];
并且我想获取“Test1”的值。但 test1 的值变为“null”.. 我如何从 app.config 文件中获取“test1”的值。 我创建了 app.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>
请帮助我..
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Web.config:
代码:
Web.config:
Code:
在 ASP.NET 应用程序中,默认配置文件名为 web.config。这是您应该遵守的约定,它允许您轻松使用
ConfigurationManager
访问配置设置。我建议看看 http://en.wikipedia.org/wiki/Web.config 作为学习 ASP.NET 域中基本 .NET 应用程序配置细节的起点。
您可以通过设置要覆盖的配置节的
file
属性将配置文件链接在一起:http://www.codeproject.com/KB/dotnet/appsettings_fileattribute.aspxIn 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如果您想在 Web 应用程序中使用 ConfigurationManager.AppSettings,则必须将 AppSettings 部分放入 web.config 中。
If you want to use ConfigurationManager.AppSettings inside a web application, you have to put your AppSettings section in web.config.