且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-11-04 19:19:02

我有点找到了解决方案.弄清这一点的关键是意识到AppDomain.CurrentDomain.SetupInformation.ConfigurationFile属性不是指向web.config文件,而是指向运行该网站的可执行文件的exe.config文件.请记住,在.net核心下,该网站以其自己的进程运行,并且具有自己的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 Project(使用完整框架)包含一个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.