<bdo id='6WWq2'></bdo><ul id='6WWq2'></ul>

  1. <tfoot id='6WWq2'></tfoot>

      <small id='6WWq2'></small><noframes id='6WWq2'>

      <legend id='6WWq2'><style id='6WWq2'><dir id='6WWq2'><q id='6WWq2'></q></dir></style></legend>

    1. <i id='6WWq2'><tr id='6WWq2'><dt id='6WWq2'><q id='6WWq2'><span id='6WWq2'><b id='6WWq2'><form id='6WWq2'><ins id='6WWq2'></ins><ul id='6WWq2'></ul><sub id='6WWq2'></sub></form><legend id='6WWq2'></legend><bdo id='6WWq2'><pre id='6WWq2'><center id='6WWq2'></center></pre></bdo></b><th id='6WWq2'></th></span></q></dt></tr></i><div id='6WWq2'><tfoot id='6WWq2'></tfoot><dl id='6WWq2'><fieldset id='6WWq2'></fieldset></dl></div>

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

      Access Web.config settings in Asp.Net Core App?(在 Asp.Net Core App 中访问 Web.config 设置?)

        <small id='JhkOl'></small><noframes id='JhkOl'>

      • <i id='JhkOl'><tr id='JhkOl'><dt id='JhkOl'><q id='JhkOl'><span id='JhkOl'><b id='JhkOl'><form id='JhkOl'><ins id='JhkOl'></ins><ul id='JhkOl'></ul><sub id='JhkOl'></sub></form><legend id='JhkOl'></legend><bdo id='JhkOl'><pre id='JhkOl'><center id='JhkOl'></center></pre></bdo></b><th id='JhkOl'></th></span></q></dt></tr></i><div id='JhkOl'><tfoot id='JhkOl'></tfoot><dl id='JhkOl'><fieldset id='JhkOl'></fieldset></dl></div>
        <legend id='JhkOl'><style id='JhkOl'><dir id='JhkOl'><q id='JhkOl'></q></dir></style></legend>
          • <bdo id='JhkOl'></bdo><ul id='JhkOl'></ul>

          • <tfoot id='JhkOl'></tfoot>

              <tbody id='JhkOl'></tbody>

              1. 本文介绍了在 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 设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to keep the Text of a Read only TextBox after PostBack()?(PostBack()之后如何保留只读文本框的文本?)
                Multiline TextBox multiple newline(多行文本框多个换行符)
                asp:TextBox ReadOnly=true or Enabled=false?(asp:TextBox ReadOnly=true 还是 Enabled=false?)
                How to set the value of a textbox textmode=date ASP.NET C#(如何设置文本框 textmode=date ASP.NET C# 的值)
                How to make an ASP.NET TextBox fire it#39;s onTextChanged event fire in an AJAX UpdatePanel?(如何让 ASP.NET TextBox 触发它在 AJAX UpdatePanel 中触发 onTextChanged 事件?)
                How to dynamically generate a TextBox control.(如何动态生成 TextBox 控件.)
                  <bdo id='D0iIw'></bdo><ul id='D0iIw'></ul>

                        <legend id='D0iIw'><style id='D0iIw'><dir id='D0iIw'><q id='D0iIw'></q></dir></style></legend>
                        <i id='D0iIw'><tr id='D0iIw'><dt id='D0iIw'><q id='D0iIw'><span id='D0iIw'><b id='D0iIw'><form id='D0iIw'><ins id='D0iIw'></ins><ul id='D0iIw'></ul><sub id='D0iIw'></sub></form><legend id='D0iIw'></legend><bdo id='D0iIw'><pre id='D0iIw'><center id='D0iIw'></center></pre></bdo></b><th id='D0iIw'></th></span></q></dt></tr></i><div id='D0iIw'><tfoot id='D0iIw'></tfoot><dl id='D0iIw'><fieldset id='D0iIw'></fieldset></dl></div>

                          <small id='D0iIw'></small><noframes id='D0iIw'>

                        1. <tfoot id='D0iIw'></tfoot>
                            <tbody id='D0iIw'></tbody>