如何检索的web.config的Web应用程序的子文件夹设置 [英] How to retrieve web.config setting in web application subfolder

查看:135
本文介绍了如何检索的web.config的Web应用程序的子文件夹设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的web应用程序的子文件夹。这不是一个单独的应用程序,只是一个子文件夹,但它有自己的web.config。有一个在该子文件夹,需要在自己的文件夹中的web.config文件访问设置一个aspx页面。

I have a subfolder in my web application. It's not a separate application, just a subfolder, but it has its own web.config. There is an aspx page in that subfolder which needs to access a setting in the web.config file in its own folder.

当我从aspx页面上的静态初始化调用ConfigurationManager.GetSection(settingname),则返回null。为什么会出现这种情况?难道说我的设置名称错误或应用程序根web.config被访问,而不是子文件夹的web.config的?

When I call ConfigurationManager.GetSection("settingname") from a static initializer on that aspx page, it returns null. Why might this happen? Could it be that my setting name is wrong or that the application root web.config is being accessed instead of the subfolder's web.config?

推荐答案

如果你想编程访问的ConnectionStrings,AppSettings的或其他任何东西比在应用程序的根目录以外的web.config文件,那么你需要使用WebConfigurationManager在System.Web.Configuration命名空间类(见的http://msdn.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager.aspx)而不是在System.Configuration命名空间ConfigurationManager中的类。这应该解决您的问题。

If you want programmatic access to ConnectionStrings, AppSettings or anything else in a web.config file other than in the root of the application then you need to use the WebConfigurationManager class in the System.Web.Configuration namespace (see http://msdn.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager.aspx) instead of the ConfigurationManager class in the System.Configuration namespace. This should resolve your problem.

从MSDN -

使用WebConfigurationManager是preferred的方式一起工作
  有关Web应用程序配置文件。对于客户端
  应用程序,使用ConfigurationManager中类

"Using WebConfigurationManager is the preferred way to work with configuration files related to Web applications. For client applications, use the ConfigurationManager class."

据我了解(虽然我不能举这个为事实)的ConfigurationManager中类专为Windows窗体,我相信这是只有一个App.config文件的应用程序。

I understand (although I can't cite this as fact) that the ConfigurationManager class was designed for Windows Forms applications where I believe there is only a single App.config file.

这篇关于如何检索的web.config的Web应用程序的子文件夹设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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