简体   繁体   English

将Asp.net Core中的ConfigurationManager与.NET Framework网站一起使用

[英]Using ConfigurationManager in Asp.net Core with .NET Framework website

When you create an ASP.NET Core website you have the option of using .NET Core or .NET Framework, depending on (among other things) whether or not your site will use .NET Framework components. 创建ASP.NET Core网站时,您可以选择使用.NET Core或.NET Framework,这取决于(其中包括)您的站点是否将使用.NET Framework组件。

I need to reference a regular .NET Framework class library in my site, so I chose the .NET Framework option. 我需要在站点中引用常规的.NET Framework类库,因此我选择了.NET Framework选项。

However, this class library makes use of appSettings defined in the app.config file. 但是,此类库使用app.config文件中定义的appSettings。 Now, if I place an app.config file in my ASP.NET Core website, and create an appSettings section and populate it with the fields I need, everything works. 现在,如果我在我的ASP.NET Core网站中放置一个app.config文件,并创建一个appSettings部分,并用我需要的字段填充它,那么一切正常。

However, these are sensitive keys (connection strings, etc.) so I want to be able to put them in an external file as described here: http://www.hanselman.com/blog/BestPracticesForPrivateConfigDataAndConnectionStringsInConfigurationInASPNETAndAzure.aspx 但是,这些是敏感键(连接字符串等),因此我希望能够将它们放在如下所述的外部文件中: http : //www.hanselman.com/blog/BestPracticesForPrivateConfigDataAndConnectionStringsInConfigurationInASPNETAndAzure.aspx

However, when I do this, I get an exception in the external library that the configuration setting is empty. 但是,当我这样做时,我在外部库中得到一个例外,即配置设置为空。 I referenced the external file, which is in the same directory as the app.config in the root of the project (not the wwwroot folder) but the value always comes up empty. 我引用了外部文件,该文件与项目根目录(而不是wwwroot文件夹)中的app.config位于同一目录中,但该值始终为空。 I tried putting the external config file in the wwwroot folder just out of desperation but that didn't work either. 我试图将外部配置文件放在wwwroot文件夹中只是出于绝望,但这也不起作用。

The external reference works fine in the .NET Framework project as well as a console app I used to test it;only the .NET Core website seems to be unable to be able to handle it as an external reference. 外部引用在.NET Framework项目以及我用来测试它的控制台应用程序中都可以正常工作;只有.NET Core网站似乎无法将其作为外部引用来处理。 It only works if it's inside the app.config file directly. 仅当它直接位于app.config文件中时,它才有效。

Is pointing the configSource or file option for appSettings in app.config not supported in ASP.NET Core, or am I missing a step? 是否在ASP.NET Core中不指向app.config中appSettings的configSource或file选项,还是我错过了一步?

If it can't be done, how can I pass the necessary configuration over to the external class library which was not built with .net core? 如果无法完成,如何将必要的配置传递给不是使用.net core构建的外部类库?

EDIT: In summary: the class library is using System.Configuration, and NOT targeting .net core or standard. 编辑:总结:类库正在使用System.Configuration,而不是针对.net核心或标准。

I'm using a website in ASP.NET core on .NET Framework, and my problem is that app.config works, but ONLY if the appSettings are in the app.config file. 我在.NET Framework的ASP.NET核心中使用的网站,我的问题是app.config可以工作,但是仅当appSettings位于app.config文件中时才可以。

I want to move them to an external file (so the appSetting values are not checked into source) but this appears to not work. 我想将它们移动到一个外部文件(这样就不会将appSetting值签入源文件),但这似乎无法正常工作。

My problem isn't that configuration doesn't work, it's that I can't put their values in a separate file. 我的问题不是配置不起作用,而是我无法将它们的值放在单独的文件中。

.NET Core as of right now doesn't support System.Configuration , so you can't use the same approach as .NET Framework here. .NET Core目前不支持System.Configuration ,因此您不能在此使用与.NET Framework相同的方法。 See https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets for guidance on securing sensitive information in configuration during development 请参阅https://docs.microsoft.com/zh-cn/aspnet/core/security/app-secrets ,以获取有关在开发过程中保护配置中敏感信息的指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM