读取虚拟目录中的 web.config 文件
我有一个 ASP.NET 应用程序(根应用程序),它有一个设置为另一个 ASP.NET 应用程序(虚拟应用程序)的虚拟目录。如何使虚拟应用程序从根应用程序的 web.config 文件中读取值?我正在查看 WebConfigurationManager.OpenWebConfiguration() 类,但我不确定如何告诉它从根向上一级。例如,我会告诉它转到 ~/web.config 以获取虚拟应用程序的 web.config 文件,但我需要它再上一层到根应用程序的文件结构。这是正确的方法吗?
任何帮助将不胜感激!
I have an ASP.NET application (Root Application) that has a virtual directory set up to another ASP.NET application (Virtual Application). How can I make the Virtual Application read values from the Root Application's web.config file? I was looking at the WebConfigurationManager.OpenWebConfiguration() class, but I'm unsure how how to tell it to go up one level from the root. For example, I would tell it to go to ~/web.config to get the the Virtual Application's web.config file, but I need it to go up one more level to the Root Application's file structure. Is this even the correct approach?
Any help would be greatly appreciated!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将 ExeConfigurationFileMap 类与 ConfigurationManager 一起使用,例如:
You can use the ExeConfigurationFileMap class with ConfigurationManager, like:
IIS 确实可以通过编程方式访问其配置数据(在 MSDN 和/或 Technet 上有记录)。这将是唯一受支持的路由(即,将继续跨 IIS 版本工作)。
否则,您可以破解解决方案(这两个过程都需要比平常更高的权限):
解析
appcmd.exe
的输出:例如这里:
<前><代码>> C:\Windows\system32\inetsrv\appcmd.exe 列表 vdir
VDIR“默认网站/”(物理路径:E:\ Dev \ weblocal \ XYZ)
VDIR“默认网站/DevRoot/TestWebClient”(物理路径:E:\ Dev \ Tests \ ClientSideWeb)
VDIR“默认网站/主页”(物理路径:E:\ Data \ Homepages)
直接从
C:\Windows\System32\inetsrv\config 读取配置
>.IIS does have programmatic access to its configuration data (which is documented on MSDN and/or Technet). This will be the only supported route (i.e. will continue to work across IIS versions).
Otherwise you can hack a solution (both of these will require higher than usual rights for the process):
Parse the output from
appcmd.exe
:E.g. here:
Read the configuration directly from
C:\Windows\System32\inetsrv\config
.我想你会发现你想要的行为实际上是默认行为。
web.config 设置向下级联。如果您的应用程序找不到该值,它将查找下一个层次结构的 web.config。
在大多数情况下,这将允许您通过 AppSettings 等进行查找。
我不确定如果您确实需要直接访问文件而不是各种配置 api 访问方法,会发生什么。
我现在有一个类似的设置,使用配置了多个虚拟应用程序的 IIS7。
I think you will find that your desired behavior is in fact the default behavior.
web.config settings cascade down. Your app will look up to the next hierarchical web.config if it can't find the value.
This would allow you to just look up via AppSettings etc. for most cases.
I'm not sure what happens if you really need to direct access to the file as opposed to the various config api access methods.
I have a setup like this right now using IIS7 with multiple virtual apps configured.
我再次尝试了 HectorMac 的建议,但它仍然对我不起作用。因此,我将寻找一种替代方法来将我的值存储在 web.config 文件中。
I tried HectorMac's suggestion again and it still doesn't work for me. As a result I am going to seek an alternative to storing my value in the web.config file.