asp.net-core - 如何使 ASP.Net 5 应用程序能够使用类库?

标签 asp.net-core visual-studio-2015

步骤 1:打开 VS 2015 RC 并创建一个新的“ASP.Net Web 应用程序”
enter image description here
enter image description here
第二步:右键解决方案,在windows中添加一个新的“类库”(一个普通的,不是“类库(Package)”)
enter image description here

第三步:在类库中的Class1.cs中放入一个方法。什么都无所谓。

第 4 步:右键单击 Web 项目中的“引用”并添加对您的类库的引用。
enter image description here

第 5 步:从 web 项目中的代码文件中,调用您在 Class1.cs 中创建的方法

所以对我来说,Class1.cs 看起来像这样:

public class Class1
{
    public void X()
    {

    }
}

我在网络应用程序中添加了这样的代码:
        var x = new ClassLibrary1.Class1();
        x.X();

第六步:尝试编译,你会得到这个错误:

错误 CS0246 找不到类型或命名空间名称“ClassLibrary1”(您是否缺少 using 指令或程序集引用?)

要使普通类库(我有很多)与 ASP.Net 5 应用程序一起使用,必须做些什么?

最佳答案

下载您的项目并构建时,我在输出中收到以下错误:

C:\REDACTED\AspNet5Test\src\AspNet5Test\Startup.cs(26,25,26,38): DNX Core 5.0 error CS0246: The type or namespace name 'ClassLibrary1' could not be found (are you missing a using directive or an assembly reference?)

请注意,尤其是 DNX Core 5.0部分 - .Net 4.5 库(例如 ClassLibrary1)与 .Net Core 不兼容。

最简单的解决方案是从 project.json 文件中删除对 dnxcore50 的依赖。

当前的:
"frameworks": {
    "dnx451": {
        "dependencies": {
            "ClassLibrary1": "1.0.0-*"
        }
    },
    "dnxcore50": { }
},

改成:
"frameworks": {
    "dnx451": {
        "dependencies": {
            "ClassLibrary1": "1.0.0-*"
        }
    }
},

关于asp.net-core - 如何使 ASP.Net 5 应用程序能够使用类库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30677446/

相关文章:

SSIS VSTA 脚本编辑器未在 VS2015 中显示

c# - 使用 Web 发布时不要复制 SASS/LESS 文件

visual-studio - 如何从 TFS 中删除文件并从源代码管理中排除?

c# - VS2015 中的自动完成 "Committed by typing the following characters"选项在哪里?

oracle - 找不到与此版本兼容的 Entity Framework 数据库提供程序

c# - 命名空间系统中不存在类型或命名空间名称 Web

asp.net-core - 是否可以使用 Core SignalR 进行音频或视频通信

Asp.net 5 MVC 6, 为 facebook 电子邮件添加权限

c# - 为什么 .net 核心依赖注入(inject)对我不起作用?

c# - ASP.NET 核心 : ShortName in the Display attribute (DataAnnotations)