Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
734 views
in Technique[技术] by (71.8m points)

.net - How to reference an ASP.NET 5 project from another project?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 Testsproject.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...