I'm trying to add .net 45 reference to a asp.net 5 starter web project..and i'm getting this errors.
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:
dependencies
sectiondependencies
sectionI 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With