I understand that asp.net core has a new configuration system that is quite flexible and that's great. But there are things I like about the web.config based configuration system from .net 4.x. For example one can put comments in the web.config file since it's an xml file. And that for me is worth sticking with xml rather than going with the shiny new json approach. [Update: I now understand that the json approach also supports comments in the file.]
So, if I have a Asp.Net Core Web Project that targets the full framework it seems like I should be able to use the web.config based System.Configuration.ConfigurationManager.AppSettings[key]
approach to getting a setting.
But when I try, the value always comes back null (at least with IIS express using VS2015).
It should work right? Any thoughts on what I might be over looking?
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=".logsstdout" forwardWindowsAuthToken="false"/>
</system.webServer>
</configuration>
Code to access setting:
string key = "SomeSetting";
string setting = ConfigurationManager.AppSettings[key];
if (setting == null)
throw new Exception("The required configuration key " + key + " is missing. ");
UPDATE
After more research I now understand why it doesn't work but I still haven't found a way to fix it. The root cause seems to be that the ConfigurationManager is looking for the config information in a different file and not in the web.config.
This can be seen by looking at AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
property. In my case instead of pointing to the website_folderweb.config
it's instead pointing to website_folderinDebug
et461win7-x64wwwGiftOasisResponsive.exe.Config
where website_folder is the path to the folder containing my website.
The documentation and intellisense say AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
is a settable property but when I try I find that setting it does not change it's value. Very odd.
So while I now see what the issue is I can't seem to find a way to fix it.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…