在区子的Web.config [英] Sub Web.config in Area
问题描述
我工作的一个巨大的MVC 4项目。我使用的面积和它的伟大。
不过,我想,对于每个区域,使用自定义Web.config文件。
例如,这是我的结构:
根
的Web.config
|
----区域
-------- MyArea_1
-------- MyArea_2
---- Web.config文件
在我的根Web.config,我所有的连接。在我的子Web.config中,我想添加的appSettings即只关注领域
在我的子Web.config文件,例如:
<&的appSettings GT;
<添加键=MyArea_1_Param1值=1/>
<添加键=MyArea_1_Param2VALUE =2/>
<添加键=MyArea_2_Param1值=1/>
< /的appSettings>
有关的那一刻,我已经把我所有的参数的根Web.config。
我怎样才能让我的不同的Web.config文件之间的链接?
------更新--------
我试着把这个代码在我的子Web.config文件:
< XML版本=1.0编码=UTF-8 ?>
<位置路径=MyArea_1>
<&的appSettings GT;
<添加键=MyArea_1_Param1值=1/>
<添加键=MyArea_1_Param2VALUE =2/>
< /的appSettings>
< /地点>
AI把这个文件,在这里:
根
的Web.config
|
----区域
-------- MyArea_1
-------- MyArea_2
---- Web.config文件< - 在这里
我已经尝试在这里:
根
的Web.config
|
----区域
-------- MyArea_1
------------ Web.config文件
----- --- MyArea_2
但我仍然得到的NullReferenceException当我尝试读取密钥值MyArea_1_Param1
我对阅读的键值代码:
ConfigurationManager.AppSettings [MyArea_1_Param1]。的ToString()
您可以使用:
<位置路径=myarea>
< appsetttings>
<添加键=MyArea_1_Param1值=1/>
< / AppSettings的>
< /地点>
和把这些web.configs下面积目录
要访问给定的区域你的web.config,你可以使用这样的代码:
System.Web.Configuration。 WebConfigurationManager.OpenWebConfiguration(/区/ MyArea1 /查看/ Web.config文件)。AppSettings.Settings [MyArea1_Param1]。价值
I'm working on a huge MVC 4 project. I'm using Area and it's great.
But, I want, for each Areas, use a custom Web.config.
For example, this is my structure :
Root
Web.config
|
----Areas
--------MyArea_1
--------MyArea_2
----Web.config
In my root Web.config, i've all the connections. In my Sub Web.config, i want to add "appSettings" that concern only Areas.
In my sub Web.config, for example :
<appSettings>
<add key="MyArea_1_Param1" value="1"/>
<add key="MyArea_1_Param2" value="2"/>
<add key="MyArea_2_Param1" value="1"/>
</appSettings>
For the moment, i've put all of my parameters in the root Web.config.
How can I make a link between my different Web.config?
------UPDATE--------
I've try to put this code in my Sub Web.config :
<?xml version="1.0" encoding="utf-8"?>
<location path="MyArea_1">
<appSettings>
<add key="MyArea_1_Param1" value="1"/>
<add key="MyArea_1_Param2" value="2"/>
</appSettings>
</location>
A I put this file, here :
Root
Web.config
|
----Areas
--------MyArea_1
--------MyArea_2
----Web.config <- Here
I already try here to :
Root
Web.config
|
----Areas
--------MyArea_1
------------Web.config
--------MyArea_2
But i'm still getting a "NullReferenceException" when I try to read the key value "MyArea_1_Param1"
My code for read the key value :
ConfigurationManager.AppSettings["MyArea_1_Param1"].ToString()
You can use:
<location path="myarea">
<appsetttings>
<add key="MyArea_1_Param1" value="1"/>
</appsettings>
</location>
and put these web.configs under area directories
To access your web.config in given area, you can use code like this:
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Areas/MyArea1/Views/Web.config").AppSettings.Settings["MyArea1_Param1"].Value
这篇关于在区子的Web.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!