【问题标题】:Understanding Web.config Hierarchy in SubFolders了解子文件夹中的 Web.config 层次结构
【发布时间】:2015-01-20 15:34:47
【问题描述】:

在我的 Visual Studio Web 项目中,根目录是 Web.config

Web.config 文件定义了用于基本网络身份验证的网络用户密码

<configuration>
    <appSettings>
        <add key="User" value="victoria"/>
        <add key="Pwd" value="i12kissU"/>
    </appSettings>
</configuration>

我的项目使用包含多种常用方法的母版页。这是我使用上面的应用程序设置的地方:

母版页:

using System.Net;
using System.Web.Configuration;
//
public CredentialCache GetNetworkCredentials(string url) {
    var item = new CredentialCache();
    var username = WebConfigurationManager.AppSettings["User"];
    var password = WebConfigurationManager.AppSettings["Pwd"];
    item.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
    return item;
}

在每个子文件夹中,项目都有访问 SQL Server 上不同数据库的例程。因此,每个 SubFolder 都有自己的 Web.config 文件,其中包含用于这些表的唯一数据库连接字符串。

<configuration>
    <connectionStrings>
        <add name="SubFolderN" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SubFolderN;User Id=UserN;Password=PwdN" providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

这让我可以为每个 SubFolder 拉出 Connection String

子文件夹N:

ConnectionString =
  WebConfigurationManager.ConnectionStrings["SubFolderN"].ConnectionString;

到目前为止,每个SubFolder 都成功地从母版页 中提取UserPwd 值。

“从 Root 继承”是 Web.config 文件的保证属性吗?

到目前为止,结果似乎很有希望。

但是,我想与社区中知道我所看到的不是浏览器缓存的结果的人确认。我需要这种情况持续发生。

带有权威参考的链接会很棒。

【问题讨论】:

    标签: c# asp.net web-config


    【解决方案1】:

    我想我在这里找到了答案:

    10 Things ASP.NET Developers Should Know About Web.config Inheritance and Overrides

    具体来说,它提到了 ASP.NET Web.config:

    特定 ASP.NET 应用程序的 Web.config 文件位于应用程序的根目录中,其中包含适用于 Web 应用程序并通过其分支中的所有子目录向下继承的设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多