使用 ASP.NET MVC2 的区域中的多个 Web.config 文件
我有一个我认为很简单的问题。我的网站中有一个区域,我想创建一个特定于该区域的 Web.config 文件,但是无论我将此配置文件放在哪里,我在 appSettings 中设置的键都返回“null” “当我访问它们时,所以我不确定我应该做什么。这是我的网站结构:
Site
[usual site stuff like Views, Models, Controllers]
Web.config
Areas
MyArea
Web.config
[usual site stuff like Views, Models, Controllers]
我的主站点上的 web.config 看起来像...
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="MyMainKey" value="MyTestValue"/>
</appSettings>
<!--all of the standard asp.net mvc config stuff -->
</configuration>
我的区域配置看起来像
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="MyAreaConfigValue" value="MyAreaTestValue"/>
</appSettings>
<!--all of the standard asp.net mvc config stuff -->
</configuration>
在我的 Areas/MyArea/Controllers 文件夹中存在的控制器操作中,我尝试像这样访问它:
string my_area_config_val = System.Configuration.ConfigurationManager.AppSettings["MyAreaTestValue"];
但是my_area_config_val 为空。
在我的主站点(不在我的区域)中的控制器操作中,我可以执行
string my_main_config_val = System.Configuration.ConfigurationManager.AppSettings["MyTestValue"];
并且 my_main_config_val 等于“MyTestVal”,如预期的那样。
谢谢。 穆斯塔法
I have a question on something that I thought would be a simple. I have an Area in my website and I'd like to create a Web.config file specific to this area but, no matter where I put this config file, the keys that I've set up in my appSettings are all returning "null" when I access them, so I'm not sure what I'm supposed to do. Here's my website structure:
Site
[usual site stuff like Views, Models, Controllers]
Web.config
Areas
MyArea
Web.config
[usual site stuff like Views, Models, Controllers]
my web.config on the main site looks something like...
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="MyMainKey" value="MyTestValue"/>
</appSettings>
<!--all of the standard asp.net mvc config stuff -->
</configuration>
My areas config looks something like
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="MyAreaConfigValue" value="MyAreaTestValue"/>
</appSettings>
<!--all of the standard asp.net mvc config stuff -->
</configuration>
In a controller action that exists in my Areas/MyArea/Controllers folder I'm trying to access it like so:
string my_area_config_val = System.Configuration.ConfigurationManager.AppSettings["MyAreaTestValue"];
But my_area_config_val is null.
In a controller action in my main site (not in my Area) I can do
string my_main_config_val = System.Configuration.ConfigurationManager.AppSettings["MyTestValue"];
and my_main_config_val is equal to "MyTestVal", as expected.
Thanks.
Mustafa
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Web.config
文件位于Area/Views
目录中。我还没有找到任何具体的事实,但从我在做一些研究中看到的是,Areas 中的Web.config
文件中的
永远不会被使用。下面是一个包含简短声明的网站:The
Web.config
file goes in theArea/Views
dir. I have not found any concrete facts, but from what I have seen in doing some research is that the<appSettings>
fromWeb.config
files within Areas will never get used. Below is one site that has a brief statement on it: