我理解asp.net core有一个非常灵活的新配置系统,这非常棒.但我喜欢网络的某些方面.基于配置的配置系统.第四网.x、 例如,人们可以在网上发表 comments .配置文件,因为它是xml文件.对我来说,坚持使用xml而不是使用shiny 的新json方法是值得的.[Update:我现在知道json方法也支持文件中的注释.]
因此,如果我有一个以整个框架为目标的ASP.NET Core Web项目,我似乎应该能够使用基于web.config的System.Configuration.ConfigurationManager.AppSettings[key]
方法来获取设置.
但当我try 时,该值总是返回null(至少在使用VS2015的IIS express中是这样).
它应该能正常工作吗?有没有想过我可能看过头了?
Web.config
<configuration>
<appSettings>
<add key="SomeSetting" value="true"/>
</appSettings>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
</system.webServer>
</configuration>
访问设置的代码:
string key = "SomeSetting";
string setting = ConfigurationManager.AppSettings[key];
if (setting == null)
throw new Exception("The required configuration key " + key + " is missing. ");
UPDATE
这可以通过查看AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
处房产来看出.在我的例子中,它没有指向website_folder\web.config
,而是指向website_folder\bin\Debug\net461\win7-x64\wwwGiftOasisResponsive.exe.Config
,其中WebSite_Folder是包含我的网站的文件夹的路径.
文档和intellisense说AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
是一个可设置的属性,但当我try 设置时,我发现它不会改变它的值.很奇怪.
所以,虽然我现在明白了问题所在,但我似乎找不到解决问题的方法.