简体   繁体   English

访问Asp.Net Core App中的Web.config设置?

[英]Access Web.config settings in Asp.Net Core App?

I understand that asp.net core has a new configuration system that is quite flexible and that's great. 我知道asp.net核心有一个非常灵活的新配置系统,这很棒。 But there are things I like about the web.config based configuration system from .net 4.x. 但是我喜欢从.net 4.x开始的基于web.config的配置系统。 For example one can put comments in the web.config file since it's an xml file. 例如,可以将注释放在web.config文件中,因为它是一个xml文件。 And that for me is worth sticking with xml rather than going with the shiny new json approach. 对我而言,值得坚持使用xml,而不是采用闪亮的新json方法。 [ Update: I now understand that the json approach also supports comments in the file.] [ 更新:我现在明白json方法也支持文件中的注释。]

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. 因此,如果我有一个针对完整框架的Asp.Net核心Web项目,我似乎应该可以使用基于web.config的System.Configuration.ConfigurationManager.AppSettings[key]方法来获取设置。

But when I try, the value always comes back null (at least with IIS express using VS2015). 但是当我尝试时,该值总是返回null(至少在使用VS2015的IIS Express时)。

It should work right? 它应该工作正常吗? Any thoughts on what I might be over looking? 关于我可能会看到什么的任何想法?

Web.config 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>

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 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. 根本原因似乎是ConfigurationManager在不同的文件中而不是在web.config中查找配置信息。

This can be seen by looking at AppDomain.CurrentDomain.SetupInformation.ConfigurationFile property. 通过查看AppDomain.CurrentDomain.SetupInformation.ConfigurationFile属性可以看到这一点。 In my case instead of pointing to the website_folder\\web.config it's instead pointing to website_folder\\bin\\Debug\\net461\\win7-x64\\wwwGiftOasisResponsive.exe.Config where website_folder is the path to the folder containing my website. 在我的情况下,而不是指向website_folder\\web.config它而是指向website_folder\\bin\\Debug\\net461\\win7-x64\\wwwGiftOasisResponsive.exe.Config其中website_folder是包含我的网站的文件夹的路径。

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. 文档和intellisense说AppDomain.CurrentDomain.SetupInformation.ConfigurationFile是一个可设置的属性,但是当我尝试时,我发现它的设置并没有改变它的值。 Very odd. 很奇怪。

So while I now see what the issue is I can't seem to find a way to fix it. 所以,虽然我现在看到问题是什么,但我似乎无法找到解决问题的方法。

I kinda found the solution. 我找到了解决方案。 The key to figuring it out was realizing that the AppDomain.CurrentDomain.SetupInformation.ConfigurationFile property wasn't pointing to the web.config file but rather to the exe.config file for the executable running the website. 解决这个问题的关键是意识到AppDomain.CurrentDomain.SetupInformation.ConfigurationFile属性没有指向web.config文件,而是指向运行网站的可执行文件的exe.config文件。 Remember, under .net core, the website runs in its own process and it has its own exe. 请记住,在.net核心下,网站运行在自己的进程中,它有自己的exe。

So the config model that .Net 4.x uses with the ConfigurationManager is more like that of a desktop app than a 4.x web application. 因此.Net 4.x与ConfigurationManager一起使用的配置模型更像是桌面应用程序而不是4.x Web应用程序。 By that I mean that it's looking at the exe.config not the web.config. 我的意思是它在看exe.config而不是web.config。

Then I noticed that the Asp.Net Core Web Project (using the full framework) contains an app.config file much like a desktop app would. 然后我注意到Asp.Net核心Web项目(使用完整框架)包含一个app.config文件,就像桌面应用程序一样。 And it turns out that if you put your .net 4.x application config settings in that file they will get placed in the exe.config file when the exe is generated, whether for debug or for release. 事实证明,如果将.net 4.x应用程序配置设置放在该文件中,则在生成exe时,无论是用于调试还是用于发布,它们都将被放置在exe.config文件中。 Just exactly like it works with a win forms app for example. 就像它适用于win form app一样。

So the way to utilize the ConfigurationManager in an asp.net core web application that targets the full framework is to put the application setting in the app.config file rather than the web.config file. 因此,在针对完整框架的asp.net核心Web应用程序中使用ConfigurationManager的方法是将应用程序设置放在app.config文件而不是web.config文件中。 The ConfigurationManager will find them no problem. ConfigurationManager会发现它们没问题。

在此输入图像描述

While this explains a lot, it still doesn't provide that ability to actually put those settings in the web.config and access them via the ConfigurationManager. 虽然这解释了很多,但它仍然没有提供实际将这些设置放在web.config中并通过ConfigurationManager访问它们的能力。 But I'm beginning to believe that's not possible in a asp.net core web application even if it is targeting the full framework. 但我开始相信,即使是针对完整框架,在asp.net核心Web应用程序中也是不可能的。

I ran into this problem when I started publishing my asp.net core 1.1 application to IIS. 当我开始将我的asp.net核心1.1应用程序发布到IIS时,我遇到了这个问题。
There would be generated web.config file on the IIS which was overriten on publishing. IIS上会生成web.config文件,在发布时会出现问题。 To enable Windows Authentification I had to add a web.config manually to my Project. 要启用Windows身份验证,我必须手动将web.config添加到我的项目中。 This one gets published correctly to IIS: 这个正确发布到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>

I also ran into this issue. 我也遇到过这个问题。 After some investigation and reading it's like that you are able to add web.config manually but this is for providing settings for IIS (eg Authentication, ...). 经过一些调查和阅读后,您可以手动添加web.config,但这是为IIS提供设置(例如身份验证,...)。

For the appsettings or custom settings you have to work with the appsettings.json file and the new Configuration in .Net Core. 对于appsettings或自定义设置,您必须使用appsettings.json文件和.Net Core中的新配置。

Microsoft Documentation Microsoft文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM