Asp.net Web.config文件读取路径你真的清楚吗?

我们经常都在用ConfigurationManager的AppSettings和ConnectionStrings属性,当一个项目中有很多Web.config时它们的读取顺序究竟是怎么的了?也许我们可以通过实验得出一些结论,但我这里仅从源代码上来分析一下。

无论是ConfigurationManager的AppSettings还是ConnectionStrings属性都在调用方一个共同的方法GetSection。从GetSection方法我们知道他主要是调用IInternalConfigSystem实例的GetSection方法,在这里用实例的GetSection方法之前它调用了一个PrepareConfigSystem方法。一看这个方法名称我们就知道他是为ConfigSystem做准备工作的。而它也是在调用一个EnsureConfigurationSystem方法,这个方法有一句很重要  s_configSystem = newClientConfigurationSystem();但是我们在Web中用的难道就是ClientConfigurationSystem类吗?不一定!让我们 确认一下,在web的项目中,我们添加一下代码:

   FieldInfo s_configSystem = typeof(ConfigurationManager).GetField("s_configSystem", BindingFlags.Static | BindingFlags.NonPublic);
            object clientConfigurationSystem  = s_configSystem.GetValue(null);

运行结果表明IInternalConfigSystem的实例是一个HttpConfigurationSystem类型。我们大家应该还知道http处理的开始是在HttpRuntime的

public static void ProcessRequest(HttpWorkerRequest wr)->private void ProcessRequestInternal(HttpWorkerRequest wr) ->private void EnsureFirstRequestInit(HttpContext context)->private void FirstRequestInit(HttpContext context)->private static void InitHttpConfiguration()->  HttpConfigurationSystem.EnsureInit(null, true, true).这里我想能说明IInternalConfigSystem的实例类型是HttpConfigurationSystem

 这段代码主要是实例化了一个HttpConfigurationSystem,InternalConfigSettingsFactory,其中有一句s_configSettingsFactory.SetConfigurationSystem(internalConfigSystem, initComplete)很是重要,让我们来看看InternalConfigSettingsFactory的SetConfigurationSystem方法:

void IInternalConfigSettingsFactory.SetConfigurationSystem(IInternalConfigSystem configSystem, bool initComplete)
    {
        ConfigurationManager.SetConfigurationSystem(configSystem, initComplete);
    }

其中ConfigurationManager.SetConfigurationSystem有这么一句

internal static void SetConfigurationSystem(IInternalConfigSystem configSystem, bool initComplete)
{
        s_configSystem = configSystem;       
}
我想大家现在应该明白为什么IInternalConfigSystem这里是HttpConfigurationSystem了吧。
现在让我们来看看HttpConfigurationSystem的GetSection方法

internal static object GetSection(string sectionName)
{
    HttpContext current = HttpContext.Current;
    if (current != null)
    {
        return current.GetSection(sectionName);
    }
    return GetApplicationSection(sectionName);
}
它实际上是调用的HttpContext 的GetSection方法:

public object GetSection(string sectionName)
{
    if (HttpConfigurationSystem.UseHttpConfigurationSystem)
    {
        return this.GetConfigurationPathData().ConfigRecord.GetSection(sectionName);
    }
    return ConfigurationManager.GetSection(sectionName);
}
最终调用的是CachedPathData的GetVirtualPathData,其调用参数依次是 CachedPathData.GetVirtualPathData(this._request.FilePathObject, false),如果这个有返回值就直接返回,否则继续调用CachedPathData.GetVirtualPathData(this._configurationPath, true)

 这个方法主要是调用GetConfigPathData方法,参数一次是("machine/webroot");(WebConfigurationHost.GetConfigPathFromSiteIDAndVPath(HostingEnvironment.SiteID, virtualPath));virtualPath));,HostingEnvironment.AppConfigPath,要想知道

(WebConfigurationHost.GetConfigPathFromSiteIDAndVPath(HostingEnvironment.SiteID, virtualPath));virtualPath));

HostingEnvironment.AppConfigPath

这两个东西在实际的项目中返回的东西可以把以下代码放到web项目中就可以知道它们具体是说明东西了。
            MethodInfo m = Type.GetType("System.Web.Configuration.WebConfigurationHost,System.Web,Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
                .GetMethod("GetConfigPathFromSiteIDAndVPath", BindingFlags.Static | BindingFlags.NonPublic);
            PropertyInfo sitID = typeof(HostingEnvironment).GetProperty("SiteID", BindingFlags.Static | BindingFlags.NonPublic);
            object SitID = sitID.GetValue(null, new object[] { });
            PropertyInfo filePathObject = typeof(HttpRequest).GetProperty("FilePathObject", BindingFlags.NonPublic | BindingFlags.Instance);
            object FilePathObject = filePathObject.GetValue(System.Web.HttpContext.Current.Request, new object[]{});
            object obj = m.Invoke(null, new object[] { SitID, FilePathObject });

            PropertyInfo appConfigPath = typeof(HostingEnvironment).GetProperty("AppConfigPath", BindingFlags.NonPublic | BindingFlags.Static);
            object AppConfigPath = appConfigPath.GetValue(null, new object[] { });

有关CachedPathData 的
private static CachedPathData GetConfigPathData(string configPath)方法具体实现我们及忽略它吧,很是复杂。最主要是这些方法都是内部私有方法,知道了也没多大意义。要想改变web.config的读取路径,理论上很简单就是自己实现一个IInternalConfigSystem子类来修改读取路径不过这个类的修改非常庞大,我们退而求次来改变Request的_filePath变量来实现修改读取路径的方法
具体的实现代码如下:

void MvcApplication_PostMapRequestHandler(object sender, EventArgs e)
      {
          HttpContextBase context = new HttpContextWrapper(((HttpApplication)sender).Context);
          string channelName = context.Request.RequestContext.RouteData.Values["ChannelName"].ToString();
          Type typeVirtualPath = Type.GetType("System.Web.VirtualPath,System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
          Type typeCachedPathData = Type.GetType("System.Web.CachedPathData,System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
          MethodInfo Create = typeVirtualPath.GetMethod("Create", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(string) }, null);
 
          PropertyInfo _filePathObject = typeof(HttpRequest).GetProperty("FilePathObject", BindingFlags.NonPublic | BindingFlags.Instance);
          object filePathObject = _filePathObject.GetValue(System.Web.HttpContext.Current.Request, new object[] { });
 
          PropertyInfo _virtualPathString = typeVirtualPath.GetProperty("VirtualPathString");
          object virtualPathString = _virtualPathString.GetValue(filePathObject, new object[] { });
          string newPathString = virtualPathString.ToString().Replace(channelName, "Views/Channel/" + channelName);
 
          object newVirtualPathString = Create.Invoke(null, new object[] { newPathString });
 
          FieldInfo filePath = typeof(HttpRequest).GetField("_filePath", BindingFlags.NonPublic | BindingFlags.Instance);
          filePath.SetValue(System.Web.HttpContext.Current.Request, newVirtualPathString);
 
          object filePathData = typeCachedPathData.GetMethod("GetVirtualPathData", BindingFlags.Static | BindingFlags.NonPublic)
              .Invoke(null, new object[] { newVirtualPathString, false });
 
          FieldInfo _filePathData = typeof(HttpContext).GetField("_filePathData", BindingFlags.NonPublic | BindingFlags.Instance);
          _filePathData.SetValue(HttpContext.Current, filePathData);
          //string aa = ConfigurationManager.AppSettings["TestData"].Trim();
      }

 其实个人并不是很建议大家来修改默认的ConfigurationManager来实现该功能。如果需要实现建议大家用WebConfigurationManager来实现。

   var str= WebConfigurationManager.OpenWebConfiguration("/Views/Channel/men/web.config").AppSettings.Settings["TestData"].Value;

看看这个是不是很简单啊。具体的代码http://download.csdn.net/detail/dz45693/4774677

posted on   dz45693  阅读(6015)  评论(8编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2012年11月 >
28 29 30 31 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示