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

时间:2021-10-25 20:08:39

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.]

据我所知,asp.net核心有一个非常灵活的新配置系统,非常棒。但是我喜欢从.net 4.x开始的基于web.config的配置系统。例如,可以将注释放在web.config文件中,因为它是一个xml文件。对我而言,值得坚持使用xml,而不是采用闪亮的新json方法。 [更新:我现在明白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
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. 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.

通过查看AppDomain.CurrentDomain.SetupInformation.ConfigurationFile属性可以看到这一点。在我的情况下,而不是指向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. Very odd.

文档和intellisense说AppDomain.CurrentDomain.SetupInformation.ConfigurationFile是一个可设置的属性,但是当我尝试时,我发现它的设置并没有改变它的值。很奇怪。

So while I now see what the issue is I can't seem to find a way to fix it.

因此,虽然我现在看到问题是什么,但我似乎无法找到解决问题的方法。

3 个解决方案

#1


22  

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. Remember, under .net core, the website runs in its own process and it has its own exe.

我找到了解决方案。解决问题的关键是意识到AppDomain.CurrentDomain.SetupInformation.ConfigurationFile属性没有指向web.config文件,而是指向运行网站的可执行文件的exe.config文件。请记住,在.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. By that I mean that it's looking at the exe.config not the web.config.

因此.Net 4.x与ConfigurationManager一起使用的配置模型更像是桌面应用程序而不是4.x Web应用程序。我的意思是它正在查看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. 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. Just exactly like it works with a win forms app for example.

然后我注意到Asp.Net核心Web项目(使用完整框架)包含一个app.config文件,就像桌面应用程序一样。事实证明,如果将.net 4.x应用程序配置设置放在该文件中,则在生成exe时,无论是用于调试还是用于发布,它们都将被放置在exe.config文件中。就像它适用于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. The ConfigurationManager will find them no problem.

因此,在面向完整框架的asp.net核心Web应用程序中使用ConfigurationManager的方法是将应用程序设置放在app.config文件而不是web.config文件中。 ConfigurationManager会发现它们没问题。

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

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

虽然这解释了很多,但仍然没有提供将这些设置实际放入web.config并通过ConfigurationManager访问它们的能力。但是我开始相信在asp.net核心Web应用程序中这是不可能的,即使它是针对完整框架的。

#2


2  

I ran into this problem when I started publishing my asp.net core 1.1 application to IIS.
There would be generated web.config file on the IIS which was overriten on publishing. To enable Windows Authentification I had to add a web.config manually to my Project. This one gets published correctly to IIS:

当我开始将我的asp.net核心1.1应用程序发布到IIS时,我遇到了这个问题。 IIS上会生成web.config文件,在发布时会出现问题。要启用Windows身份验证,我必须手动将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>

#3


0  

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 (e.g 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文档

#1


22  

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. Remember, under .net core, the website runs in its own process and it has its own exe.

我找到了解决方案。解决问题的关键是意识到AppDomain.CurrentDomain.SetupInformation.ConfigurationFile属性没有指向web.config文件,而是指向运行网站的可执行文件的exe.config文件。请记住,在.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. By that I mean that it's looking at the exe.config not the web.config.

因此.Net 4.x与ConfigurationManager一起使用的配置模型更像是桌面应用程序而不是4.x Web应用程序。我的意思是它正在查看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. 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. Just exactly like it works with a win forms app for example.

然后我注意到Asp.Net核心Web项目(使用完整框架)包含一个app.config文件,就像桌面应用程序一样。事实证明,如果将.net 4.x应用程序配置设置放在该文件中,则在生成exe时,无论是用于调试还是用于发布,它们都将被放置在exe.config文件中。就像它适用于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. The ConfigurationManager will find them no problem.

因此,在面向完整框架的asp.net核心Web应用程序中使用ConfigurationManager的方法是将应用程序设置放在app.config文件而不是web.config文件中。 ConfigurationManager会发现它们没问题。

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

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

虽然这解释了很多,但仍然没有提供将这些设置实际放入web.config并通过ConfigurationManager访问它们的能力。但是我开始相信在asp.net核心Web应用程序中这是不可能的,即使它是针对完整框架的。

#2


2  

I ran into this problem when I started publishing my asp.net core 1.1 application to IIS.
There would be generated web.config file on the IIS which was overriten on publishing. To enable Windows Authentification I had to add a web.config manually to my Project. This one gets published correctly to IIS:

当我开始将我的asp.net核心1.1应用程序发布到IIS时,我遇到了这个问题。 IIS上会生成web.config文件,在发布时会出现问题。要启用Windows身份验证,我必须手动将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>

#3


0  

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 (e.g 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文档