【问题标题】:ASP.NET Core 3.1 project that is referencing a .NET 4.7.2 project in the same solution error 'System.Web.Configuration.WebConfigurationManager'在同一解决方案错误“System.Web.Configuration.WebConfigurationManager”中引用 .NET 4.7.2 项目的 ASP.NET Core 3.1 项目
【发布时间】:2020-04-13 18:57:15
【问题描述】:

我有一个 ASP.NET Core 3.1 项目,它在同一解决方案中引用了一个 .NET 4.7.2 项目。它编译没有问题,但在运行时出现错误:

无法加载类型“System.Web.Configuration.WebConfigurationManager”。

我已经在 ASP.NET Core 项目中安装了 Microsoft.Windows.Compatibility 包,但这没有帮助。

如果不对 .NET 4.7.2 项目进行一些重大重构以不使用 System.Web.Configuration.WebConfigurationManager 命名空间,我是否遗漏了什么或者这将无法工作?

【问题讨论】:

    标签: asp.net asp.net-core visual-studio-2019 asp.net-core-3.1


    【解决方案1】:

    我有一个 ASP.NET Core 3.1 项目,它在同一解决方案中引用了一个 .NET 4.7.2 项目。它编译没有问题,但在运行时出现错误:

    无法加载类型“System.Web.Configuration.WebConfigurationManager”

    ASP.NET Core 是一个跨平台的框架,ASP.NET Core 使用不同的配置设置,更多信息请查看:

    我想您的 .NET 4.7 项目可能是一个类库并且会被其他应用程序调用。

    如上错误指出System.Web.Configuration.WebConfigurationManager 无法与.NET Core 应用程序兼容。

    如果可能,您可以尝试修改您的类库(.NET 4.7 项目)以将检索到的数据(您在 web.config 或 appsettings.json 中配置)从调用方应用程序传递到类库方法,而不是通过WebConfigurationManager 在类库中。

    此外,您还可以修改和添加其他方法以使其与 ASP.NET Core 配置设置一起使用,如下所示。

    public class MyClassLibrary
    {
        private readonly IConfiguration Configuration;
        public MyClassLibrary()
        {
        }
        public MyClassLibrary(IConfiguration configuration)
        {
            Configuration = configuration;
        }
    
        public void MethodForNet()
        {
            var val = WebConfigurationManager.AppSettings["mykey"];
    
            //code logic here
        }
    
        public void MethodForCore()
        {
            var val = Configuration["AppSettings:mykey"];
    
            //code logic here
        }
    
        //other methods
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多