Por*_*koi 5 c# web-config asp.net-mvc-4
我正在做一个庞大的MVC 4项目。我正在使用Area,它很棒。
但是,我想针对每个区域使用自定义Web.config。
例如,这是我的结构:
Root
Web.config
|
----Areas
--------MyArea_1
--------MyArea_2
----Web.config
在我的根Web.config中,我具有所有连接。在我的Sub Web.config中,我想添加仅涉及Areas的“ appSettings”。
在我的子Web.config中,例如:
<appSettings>
    <add key="MyArea_1_Param1" value="1"/>
    <add key="MyArea_1_Param2" value="2"/>
    <add key="MyArea_2_Param1" value="1"/>
</appSettings>
目前,我已将所有参数都放在根Web.config中。
如何在不同的Web.config之间建立链接?
------更新--------
我尝试将这段代码放在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>
AI将这个文件放在这里:
Root
Web.config
|
----Areas
--------MyArea_1
--------MyArea_2
----Web.config <- Here
我已经在这里尝试:
Root
Web.config
|
----Areas
--------MyArea_1
------------Web.config
--------MyArea_2
但是当我尝试读取键值“ MyArea_1_Param1”时,我仍然收到“ NullReferenceException”
我的代码读取键值:
ConfigurationManager.AppSettings["MyArea_1_Param1"].ToString()
您可以使用:
<location path="myarea">
  <appsetttings>
    <add key="MyArea_1_Param1" value="1"/>
  </appsettings>
</location>
并将这些 web.configs 放在区域目录下
要在给定区域访问您的 web.config,您可以使用如下代码:
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Areas/MyArea1/Views/Web.config").AppSettings.Settings["MyArea1_Param1"].Value
| 归档时间: | 
 | 
| 查看次数: | 7708 次 | 
| 最近记录: |