.NET 4.7.1 was supposed to solve problems we had in referencing netstandard 2.0
libraries from the full framework. It sort of did, despite some continuing and painful dll conflict warnings and related problems, and the need to manually update to PackageReferences (see this wondeful extension). Nonetheless, one can get it working, though see note 1 below, and if I may say: it is unfortunate to say the least that there has been no VStudio help or much guidance on this and related issues, till now one has to find such help on back channels on github. This very problem itself ideally would have been messaged: ASP.NET MVC 5 does NOT yet support netstandard in razor ... wish they would just have messaged us that if true! Would save endless wasted hours! But is that the case? Or is there a fix?
Here then is the problem with ASP.NET MVC 5 projects (even those targeting 4.7.1). Although plain .cs code works, including in controllers, this is not true for any code within razor views (.cshtml
files). Any types referenced within razor views that came from a netstandard
library completely fail.
To reproduce this problem and make sure it wasn't just my own code, I reproduced this by making a bran new ASP.NET MVC 5 project (on github) in the newest version of VStudio 2017 (even the Preview version, 15.7.0 Preview 4.0), then making a new netstandard
project with just a couple of types in it, so I could practice referencing those types in the MVC 5 view pages. And sure enough, it still fails. For instance, this simple type from the netstandard project:
public enum AnimalType { Cat, Dog, Zebra, Alligator }
If you make that enum a type within your view model passed into the page, if you ever reference that property in the razor page, you will get compile time errors and also at runtime, saying:
The type 'Enum' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. AspMvc5WebApp471
As also detailed in that repo, I even tried to recompile my own version of Microsoft.CodeDom.Providers.DotNetCompilerPlatform
to reference it within the web.config
, but that didn't solve the problem.
So it would be lovely to hear from the ASP.NET team or anyone else that might know how to fix this problem, what that fix might entail. Or if ASP.NET MVC 5 simply is not yet workable with netstandard
, it would be nice to know if that is the message, and if support for netstandard
might be coming to ASP.NET MVC 5 in the near future, or if it is on a roadmap somewhere? And perhaps what exactly is the thing causing this failure? Of course, it would be happiest to hear if there is a fix that can be applied right away, but either way, we need to know, otherwise netstandard
is basically useless for those of us who can't just dump ASP.NET MVC 5 in a day (as much as we would like to, in the real world...) Much appreciated.
(Note 1: Net Framework 4.7.1 was eagerly looked forward to by myself and many others when it was said for some time it would solve many of the pain points, but unfortunately it has introduced its own set of endless dll hell like problems, or see here, or here, or here. See for instance the discussions on System.Net.Http
(and binding redirects do not just remove all of the conflict warnings, they often bring up their own, highly painful stuff). Now some have been hoping 4.7.2 would solve all these problems, though it didn't solve these ASP.NET MVC 5 problems for me)
I looked at the solution (awesome explanation @Nicholas Petersen !!) and I see that at Razor compilation time you are missing the reference to netstandard.dll (which is what the error is about).
I added it to the list of assemblies used during compilation like this:
The section I now have looks like this:
With that change I was able to get both the IDE to show me Intellisense for the Model in the cshtml file and the page rendered!
This is running on .NET Framework 4.7.1 and I expect this to work on .NET Framework 4.7.2. as well.
Note: There will still be a red squiggle in the IDE with the error message about the Enum type missing. But as that does not impact functionality (the Intellisense works and the code runs) I hope that will be acceptable for now.