在 Asp.Net Core App 中访问 Web.config 设置? [英] Access Web.config settings in Asp.Net Core App?

查看:19
本文介绍了在 Asp.Net Core App 中访问 Web.config 设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解 asp.net core 有一个新的配置系统,它非常灵活,非常棒.但是我喜欢 .net 4.x 中基于 web.config 的配置系统.例如,可以在 web.config 文件中添加注释,因为它是一个 xml 文件.这对我来说值得坚持使用 xml 而不是使用闪亮的新 json 方法.[更新:我现在明白 json 方法也支持文件中的注释.]

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 Core Web 项目,我似乎应该能够使用基于 web.config 的 System.Configuration.ConfigurationManager.AppSettings[key] 获取设置的方法.

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.

但是当我尝试时,该值总是返回 null(至少在 IIS express 使用 VS2015 时).

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>

访问设置的代码:

string key = "SomeSetting";
string setting = ConfigurationManager.AppSettings[key];
if (setting == null)
       throw new Exception("The required configuration key " + key + " is missing. ");

更新
经过更多研究,我现在明白为什么它不起作用,但我仍然没有找到解决它的方法.根本原因似乎是 ConfigurationManager 在不同的文件中而不是在 web.config 中查找配置信息.

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.

这可以通过查看 AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 属性来查看.在我的情况下,而不是指向 website_folderweb.config 而是指向 website_folderinDebug et461win7-x64wwwGiftOasisResponsive.exe.Config 其中 website_folder是包含我的网站的文件夹的路径.

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.

文档和智能感知说 AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 是一个可设置的属性,但是当我尝试时,我发现设置它并不会改变它的值.很奇怪.

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.

推荐答案

我找到了解决方案.弄清楚它的关键是意识到 AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 属性不是指向 web.config 文件,而是指向运行网站的可执行文件的 exe.config 文件.请记住,在 .net core 下,网站运行在自己的进程中,并且有自己的 exe.

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.

因此,.Net 4.x 与 ConfigurationManager 一起使用的配置模型更像是桌面应用程序,而不是 4.x Web 应用程序.我的意思是它正在查看 exe.config 而不是 web.config.

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.

然后我注意到 Asp.Net Core Web 项目(使用完整框架)包含一个 app.config 文件,与桌面应用程序非常相似.事实证明,如果您将 .net 4.x 应用程序配置设置放在该文件中,它们将在生成 exe 时放置在 exe.config 文件中,无论是用于调试还是发布.就像它与例如 win forms 应用程序一样.

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 应用程序中使用 ConfigurationManager 的方法是将应用程序设置放在 app.config 文件中,而不是 web.config 文件中.ConfigurationManager 会发现它们没有问题.

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.

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

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.

这篇关于在 Asp.Net Core App 中访问 Web.config 设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆