c# - 了解子文件夹中的 Web.config 层次结构

标签 c# asp.net web-config

在我的 Visual Studio Web 项目中,根目录中有一个 Web.config

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

<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>

这让我可以为每个 子文件夹 提取连接字符串

子文件夹N:

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

到目前为止,每个 SubFolder 都成功地从 Master Page 中提取了 UserPwd 值。

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

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

但是,我想向社区中的某个人确认我所看到的不是浏览器缓存的结果。我需要这种情况始终如一地发生。

带有权威引用的链接会很棒。

最佳答案

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

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

具体来说,它说的是 ASP.NET Web.config:

The Web.config file for a specific ASP.NET application is located in the root directory of the application and contains settings that apply to the Web application and inherit downward through all of the subdirectories in its branch.

image

关于c# - 了解子文件夹中的 Web.config 层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28049253/

相关文章:

asp.net-mvc - 对于 MVC 应用程序来说,web.config 中的 <customErrors> 去哪里了?

asp.net - 使用 web.config 通过 ASP.NET 启用 URL 重写

c# - 插值对象碰撞卡住

c# - 如何区分用户点击链接和页面自动重定向?

c# - 是否可以忽略邻近设备消息

c# - 调用多播委托(delegate)是否分配内存?

c# - IIs 错误 : Application Codebehind =“Global.asax.cs” Inherits =“nadeem.MvcApplication”

Asp.Net System.Web.Routing 查找实际的 .aspx 页面

javascript - jquery 和 asp.net 中的全局化。如何?

web-config - 当 web.config 包含 ipSecurity 权限时允许 "localhost"在 iisexpress 中运行