这个问题似乎被问了很多,但没有一个真正为我解决问题。我有一个带有区域的 ASP.NET MVC4 应用程序。在这个区域内,我有一个带有覆盖 appsettings 的 web.config。尝试在该区域内的 Controller 中读取这些设置时,似乎不会拾取这些设置。
-MyApp
-Areas
-MyArea
-Controllers
-MyController
-Web.config
-Web.config
在
MyApp\Web.config
我有:<appSettings>
<add key="DefaultDisplayMode" value="Configuration" />
</appSettings>
在
MyApp\Areas\MyArea\Web.config
我有:<appSettings>
<add key="DefaultDisplayMode" value="Review" />
<add key="SubSetting" value="Testing" />
</appSettings>
在
MyController
如果我查询应用程序设置,我会看到:DefaultDisplayMode: Configuration
我希望看到:
DefaultDisplayMode: Review
SubSetting: Testing
我试过两者:
System.Configuration.ConfigurationManager.AppSettings["..."]
System.Web.Configuration.WebConfigurationManager.AppSettings["..."]
我什至尝试了以下(在顶部)结构,但没有运气:
-MyApp
-Areas
-MyArea
-Controllers
-MyController
-Web.config
-Web.config
-Web.config
有人知道为什么我的子目录 appSettings 不起作用吗?
最佳答案
IIS 将基于 URL 加载 web.config,而不是基于路由请求执行的代码。
您可以创建一个模拟所需路由的空文件夹结构并在那里添加配置文件。如果这不起作用,您可以使用 <location>
主配置文件中的标记 - 但位置标记中的路径也必须匹配 URL 而不是代码。
关于asp.net - MVC 4 子目录中的 web.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13290710/