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

编程入门 行业动态 更新时间:2024-10-18 22:34:08
本文介绍了访问Asp.Net Core App中的Web.config设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我了解asp核心具有一个新的配置系统,该系统非常灵活,而且很棒.但是我有些喜欢 4.x中基于web.config的配置系统.例如,可以将注释放入web.config文件中,因为它是xml文件.对我来说,值得坚持使用xml,而不是使用闪亮的json新方法. [更新:我现在了解json方法也支持文件中的注释.]

I understand that asp 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 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核心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(至少对于使用VS2015的IIS express).

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

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中查找配置信息.

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_folder\web.config,而是指向website_folder\bin\Debug\net461\win7-x64\wwwGiftOasisResponsive.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_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是可设置的属性,但是当我尝试时发现它的设置不会改变它的值.很奇怪.

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文件.请记住,在核心下,该网站以其自己的进程运行,并且具有自己的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 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文件,就像桌面应用程序一样.事实证明,如果将 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 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核心Web应用程序中使用ConfigurationManager的方法是将应用程序设置放在app.config文件而不是web.config文件中. ConfigurationManager会发现它们没有问题.

So the way to utilize the ConfigurationManager in an asp 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核心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 core web application even if it is targeting the full framework.

更多推荐

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

本文发布于:2023-11-14 00:20:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585704.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Core   Net   Asp   config   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!