如何从另一个项目引用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。
在同一解决方案中,我有一个NUnit类库项目,但在此处尝试将Web项目添加为参考时,Visual Studio表示无法添加对'...'的参考。
是有解决方法吗?还是对ASP.NET vNext应用程序进行单元测试的首选方法是什么?
最简单的方法是确保ASP .NET 5 Web项目和测试项目共享一个公共父目录。例如:
解决方案
|-Web
| |-project.json
|-测试
| |-project.json
这样Roslyn将能够解析不同子目录中的符号。 / p>
当然,您还需要将 Tests 项目中的依赖项添加到 Web 项目在 Tests\project.json
文件中:
{
webroot: wwwroot,
version: 1.0.0- *,
dependencies:{
Web : 1.0.0- *
},
frameworks:{
dnx451:{},
dnxcore50:{}
}
}
如果他们没有共同的父目录,则可以始终在根目录中添加 global.json
文件,并在其中包含包含源代码的子目录。
解决方案
|-global.json
|-Web
| |-project.json
|-测试
| |-project.json
其中 global.json
包含:
{
源:[网络,测试]
}
在Visual Studio 2015中使用添加引用
如果使用Visual Studio 2015中的添加引用对话框从 ASP.NET 5 Web 项目引用类库项目,则会出现此错误:
无法添加对< ProjectName> 的引用。
这意味着您要引用的项目不是 DNX 项目,而是普通的 Class Library 项目。您需要确保该类库是一个 ASP.NET类库(包)项目,以便对其进行引用。
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.
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."
Is there a workaround for this? Or what is the preferred way of unit testing ASP.NET vNext applications?
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
That way Roslyn will be able to resolve the symbols across different subdirectories.
Of course, you'll also need to add a dependency from the Tests project to the Web project in the Tests\project.json
file:
{
"webroot": "wwwroot",
"version": "1.0.0-*",
"dependencies": {
"Web": "1.0.0-*"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
}
}
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
where global.json
contains:
{
"sources": ["Web", "Tests"]
}
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.
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屋!