gpt4 book ai didi

c# - 区域中的子Web.config

转载 作者:太空狗 更新时间:2023-10-29 23:15:29 24 4
gpt4 key购买 nike

我正在处理一个巨大的 MVC 4 项目。我正在使用 Area,它很棒。

但是,我想为每个区域使用自定义 Web.config。

例如,这是我的结构:

Root
Web.config
|
----Areas
--------MyArea_1
--------MyArea_2
----Web.config

在我的根 Web.config 中,我拥有所有连接。在我的 Sub Web.config 中,我想添加仅涉及区域的“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>

我把这个文件放在这里:

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

关于c# - 区域中的子Web.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19220761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com