据我所知,asp.net核心有一个新的配置系统,非常灵活,而且非常棒。但是,我喜欢web.config 4.x中基于.net的配置系统。例如,可以将注释放在web.config文件中,因为它是一个xml文件。对我来说,这一点值得坚持使用xml,而不是使用闪亮的新json方法。更新:我现在了解到json方法也支持文件中的注释。
因此,如果我有一个以整个框架为目标的Asp.Net核心网络项目,我似乎应该能够使用基于web.config的System.Configuration.ConfigurationManager.AppSettings[key]方法来获得一个设置。
但是,当我尝试时,这个值总是返回null (至少在中使用VS2015)。
应该能用对吧?对我可能会错过什么有什么想法吗?
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. ");更新
经过更多的研究,我现在明白了为什么它不起作用,但我仍然没有找到解决它的方法。根本原因似乎是ConfigurationManager在另一个文件中寻找配置信息,而不是在web.config中。
通过查看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是一个可设置的属性,但是当我尝试时,我发现设置它不会改变它的值。很奇怪。
所以,当我现在看到问题是什么的时候,我似乎找不到解决问题的方法。
发布于 2017-05-10 10:54:22
当我开始将我的asp.net核心1.1应用程序发布到IIS时,我遇到了这个问题。
在IIS上会生成web.config文件,在发布时会出现过大的错误。为了启用身份验证,我必须将web.config手动添加到项目中。此文件将正确发布到IIS:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\yourproject.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
<security>
<authentication>
<anonymousAuthentication enabled="false" />
<windowsAuthentication enabled="true" />
</authentication>
</security>
</system.webServer>
</configuration>https://stackoverflow.com/questions/40186445
复制相似问题