如何在运行时动态选择Web Config [英] How to dynamically select Web Config at run time

查看:67
本文介绍了如何在运行时动态选择Web Config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在使用以下代码行在运行时动态加载web.config文件.但它只会加载根web.config.

谁能解决这个问题?

Hi All

I am using the following line of code to dynamically load the web.config file at runtime. But it only loads the root web.config.

Can any one solve this problem?

Dim a As String = Request.Url.Host.ToString()
        If Request.Url.Host.Contains("tes1.com") Then

            Try
                Me.MasterPageFile = "~/TestLogo1.master"

                ' Set the root path of the Web application that contains the
                ' Web.config file that you want to access.
                ' Get the configuration object to access the related Web.config file.


                Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration("~/Web2.config")
                ' Get the object related to the <appsettings> section.
                Dim section As AppSettingsSection = DirectCast(config.GetSection("appSettings"), AppSettingsSection)

                ' Read the <appsettings> section.
                'StringBuilder appSettings = new StringBuilder();
                'appSettings.Append("WebConfig1");
                'appSettings.Append(section.Settings["WebConfig1"].ToString());
                Dim SettingValue As String = section.Settings("WebConfig2").Value

                ' Display the <appsettings> information.
                'ConfigId.Text = appSettings.ToString();
                Response.Write(SettingValue)
            Catch ex As Exception
                Response.Write(ex)
            End Try</appsettings></appsettings></appsettings>


在此先感谢


Thanks in Advance

推荐答案

Web.config对于每个虚拟目录应该仅是一个.即使将多个web.config放在一个文件夹中,ConfigurationManager也会仅解析文件Web.config.其他则需要手动阅读.

看看这些链接以拥有多个配置文件:
在ASP.NET Web应用程序中包含多个.Config文件 [在一个应用程序中使用多个配置文件 [ ^ ]
Web.config should be only one for each virtual directory. Even if you place multiple web.config in one folder, ConfigurationManager will only parse the file Web.config. Others you need to read it manually.

Have a look at these links to have multiple config file:
Include Multiple .Config Files in ASP.NET Web Application[^]
Using multiple config files in one application[^]


感谢sandeep的回答,我已经访问了链接,但不能解决我的问题
thanks sandeep for your answer i have already visited the links but it did not solve my problem


这篇关于如何在运行时动态选择Web Config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆