I'm trying to add .net 45 reference to a asp.net 5 starter web project..and i'm getting this errors.
- Don't you think that whatever error is correct. You added reference to .net 4.5 and you have used ASP.net 5 starter project that is based on .net 4.6 and asp.net 5.dotnetstep– dotnetstep2015-02-20 01:58:11 +00:00Commented Feb 20, 2015 at 1:58
2 Answers
While it is true that you can target multiple frameworks in a way similar to how you've done in your example, I think you will probably not be able to get the StarterWeb project to run under .NET 4.5 without some significant changes.
When dealing with multiple frameworks, Visual Studio 2015 will actually provide a lot of help if you hover the mouse over the code with errors:

One obvious difficulty in getting the StarterWeb project working is that between MVC 5 and 6, a number of pieces have been moved into new namespaces and broken up into different assemblies. Most notably, the MVC 5 package depends on System.Web, whereas the 6.0 beta that is used by the StarterWeb project does not. With the MVC libraries moved, the Authorize attribute is now in Microsoft.Aspnet.Mvc, whereas in earlier versions it belonged to System.Web.Mvc.
While in theory you could probably target different versions of MVC across the frameworks, in practice that would probably not be worth the trouble. Even if you find Authorize, there's no guarantee that it will be the "same" one, even if it compiles.
If that is a route you want to go (or just when targeting multiple frameworks in general), you have control over which packages are used by which framework in the project.json file.
If packages are compatible with all of the targeted frameworks, you can leave them in the parent dependencies area of the project.json file, but when they are specific to a given framework, you need to add a child dependencies section to that particular framework's configuration. In the case of .NET 4.5, you can also add familiar framework assemblies (as opposed to NuGet packages) by adding a frameworkAssemblies section.
You will end up with something like this:
"dependencies": { "SomeCommonPackage":"1.0.0" }, "frameworks": { "aspnet50":{ "dependencies":{ "Aspnet50SpecificPackage":"1.0.0" }, "net45":{ "dependencies":{ "NET45SpecificPackage":"1.0.0" }, "frameworkAssemblies":{ "System.Web":"4.0.0.0" } When you target these different frameworks, you will often find that at least some code will have to framework-specific. To handle this, you can add compiler directives for the various using statements and the actual code that depends on packages only available in one framework or another.
For example, you may end up with areas that look something like this:
SomeType result; #if ASPNET50 result = SomeMethodNotAvailableIn45( ); #endif #if NET45 result = EquivalentMethodIn45( ); #endif Obviously that is overly simplistic, but it gives you the basic formula:
- Put shared dependencies in the root
dependenciessection - Put dependencies specific to a given framework in its own
dependenciessection - Use compiler directives for framework-specific code that can't be shared
I also would recommend you take a look at Rick Strahl's excellent blog post to see a great walk-through of targeting multiple frameworks with more detail and lots of screenshots. One nice feature of the new project system is that it can easily create a NuGet package for all of the frameworks you choose to target, and he goes into more detail about that as well.
Comments
Adding a .NET 4.5 project that is in the same solution as a reference to a vNext project still doesn't work in VS2015 RTM/ASP.NET Beta5. The project just can't seem to find the source/binary and tries looking in nuget for it. I followed the recommended steps with global.json, dnx451 dependencies.
My work around was to
- Make the .NET 4.5 project pack a nuget package in post build step
- Add a package source location to the local package file
This way I can speed up development cycle without requiring a nuget publish. One wrinkle is that relative package source paths using a $ are replaced with absolute paths.