在ASP.NET中访问app.config

per*_*rcy 4 .net c# asp.net app-config appsettings

我正在使用app.config文件从中读取数据..我正在加载app.config文件:

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

string image_path = ConfigurationManager.AppSettings["Test1"];
Run Code Online (Sandbox Code Playgroud)

我想获得"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>
Run Code Online (Sandbox Code Playgroud)

请帮帮我..

aba*_*hev 10

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>
Run Code Online (Sandbox Code Playgroud)

码:

string image_path = ConfigurationManager.AppSettings["Test1"];
Run Code Online (Sandbox Code Playgroud)


J. *_*een 6

在ASP.NET应用程序中,默认配置文件名为web.config.这是您应该坚持的惯例,它允许您轻松使用ConfigurationManager访问配置设置.

我建议以http://en.wikipedia.org/wiki/Web.config为起点,了解ASP.NET域中基本.NET应用程序配置的细节.

您可以通过设置file要覆盖的配置节的属性将配置文件链接在一起:http://www.codeproject.com/KB/dotnet/appsettings_fileattribute.aspx