如何从另一个项目引用 ASP.NET 5 项目? [英] How to reference an ASP.NET 5 project from another project?
问题描述
我有一个从 VS2015 中的 ASP.NET 5 预览模板创建的 Web 应用程序项目.它在 dnx 1.0.0-beta7 上运行,但我使用的是完整的 .NET 框架而不是 .NET Core.
I have a Web Application project created from ASP.NET 5 Preview Templates in VS2015. It is running on dnx 1.0.0-beta7, but I'm using full .NET framework rather than .NET Core.
在同一个解决方案中,我有一个 NUnit 类库项目,但是在尝试将 Web 项目添加为此处的引用时,Visual Studio 显示无法添加对‘...’的引用."
Within the same solution I have a NUnit class library project, but when trying to add the web project as a reference here, Visual Studio says "A reference to '...' could not be added."
是否有解决方法?或者对 ASP.NET vNext 应用程序进行单元测试的首选方法是什么?
Is there a workaround for this? Or what is the preferred way of unit testing ASP.NET vNext applications?
推荐答案
最简单的方法是确保您的 ASP.NET 5 Web 项目和测试项目共享一个公共父目录.例如:
The easiest way is to make sure that your ASP.NET 5 web project and the test project share a common parent directory. For example:
Solution
|-- Web
| |-- project.json
|-- Tests
| |-- project.json
这样 Roslyn 将能够解析不同子目录中的符号.
That way Roslyn will be able to resolve the symbols across different subdirectories.
当然,您还需要将 Tests 项目中的 dependency 添加到 Tests 中的 Web 项目project.json
文件:
Of course, you'll also need to add a dependency from the Tests project to the Web project in the Testsproject.json
file:
{
"webroot": "wwwroot",
"version": "1.0.0-*",
"dependencies": {
"Web": "1.0.0-*"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
}
}
如果它们不能有共同的父目录,你总是可以在根目录中添加一个global.json
文件,其中包含包含源代码的子目录列表.
If they can't have common parent directory, you can always add a global.json
file in the root directory with the list of subdirectories that contain source code.
Solution
|-- global.json
|-- Web
| |-- project.json
|-- Tests
| |-- project.json
其中 global.json
包含:
{
"sources": ["Web", "Tests"]
}
在 Visual Studio 2015 中使用添加引用"
如果当您使用 Visual Studio 2015 中的添加引用对话框从 ASP.NET 5 Web 项目引用类库项目时,您会收到此错误:
Using "Add Reference" in Visual Studio 2015
If when you reference a class library project from an ASP.NET 5 Web project using the Add Reference dialog in Visual Studio 2015 you get this error:
无法添加对
A reference to <ProjectName> could not be added.
这意味着您尝试引用的项目不是DNX 项目,而是一个普通的类库 项目.您需要确保类库是 ASP.NET 类库(包) 项目才能引用它.
It means that the project you're trying to reference isn't a DNX project but rather a normal Class Library project. You need to make sure that the class library is an ASP.NET Class Library (Package) project in order to reference it.
这篇关于如何从另一个项目引用 ASP.NET 5 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!