18

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

Nicholas Petersen
  • 9,104
  • 7
  • 59
  • 69
  • Have you tried directly referencing netstandard.dll? – Alex Ghiondea - MSFT May 04 '18 at 13:31
  • @AlexGhiondea as referenced in the third article I referenced at the bottom (the second ['here'](https://github.com/dotnet/standard/issues/391#issuecomment-328376799)), and as you can see [here](https://www.nuget.org/packages/NETStandard.Library.NETFramework/2.0.0-preview2-25405-01), they used to have a `NETStandard.Library.NETFramework` nuget package which would basically be what you are asking (a netstandard dll), but that has been "deprecated. To consume .NET Standard libraries from .NET Framework in VS2017 15.3, you only need the .NET Core 2.0 SDK installed." – Nicholas Petersen May 04 '18 at 17:11
  • But maybe there is a reference that still needs to be made. However, why would everything else still work (all other code besides razor)? So that, along with the above point, leads me to strongly believe it is not just missing a package or something. If anything, perhaps its that `Microsoft.CodeDom.Providers.DotNetCompilerPlatform` needs to be updated, though I really don't know, and would love to have someone from the ASP.NET team or something with knowledge to chime in here – Nicholas Petersen May 04 '18 at 17:12
  • By the way, I also tried installing `NETStandard.Library` from nuget into the ASP.NET MVC project, which actually installs two things: `NETStandard.Library.2.0.2` and `Microsoft.NETCore.Platforms.1.1.0` It didn't work or do anything different. It didn't (somewhat surprisingly) throw additional errors, but nonetheless, it did nothing to help. Also I learned there is a `netstandard.dll` at this location: `C:\Program Files (x86)\Microsoft SDKs\NuGetPackagesFallback\NETStandard.Library\2.0.1\build\netstandard2.0\ref\netstandard.dll`. Referencing that didn't do anything either. – Nicholas Petersen May 04 '18 at 19:53
  • Could you add the reference to netstandard.dll like this: next to the other references? This is going to resolve netstandard from the reference assembly folder. – Alex Ghiondea - MSFT May 04 '18 at 21:39
  • thanks for the interaction. I don't remember what I did last but the csproj already had exactly that `` (and that showed up as one of the grey references in References, i.e. not a nuget one). I removed and and tried again just to make sure, didn't help. – Nicholas Petersen May 04 '18 at 22:25
  • That is interesting. Is there a small repro solution I can try on my end? – Alex Ghiondea - MSFT May 04 '18 at 23:30
  • 1
    Yes! As I said in the main post, it's here: https://github.com/copernicus365/AspMvc5NetstandardCompatibility Thanks for taking interest in this problem! – Nicholas Petersen May 04 '18 at 23:53

1 Answers1

25

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:

<add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />

The section I now have looks like this:

<system.web>
    <compilation>
        <assemblies>
            <add assembly="System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            <add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
        </assemblies>
    </compilation>
</system.web>

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!

enter image description here

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.

Rudey
  • 4,717
  • 4
  • 42
  • 84
Alex Ghiondea - MSFT
  • 3,182
  • 1
  • 8
  • 11
  • 2
    You, sir, are to be greatly thanked, beyond words. I just tested with the update and it works! I don't know how in the world you found this obscure fix, but it seems to be working in my local tests. Furthermore, I don't know what the difference is, but it doesn't seem to be showing me the red squiggly errors in the cshtml page (in the IDE). Next week I will post an update to the repo with your fix, and maybe we can see then if any changes I had made (related to the conversation we were having) might have fixed the IDE errors too, compared to what you are seeing. – Nicholas Petersen May 05 '18 at 06:59
  • Furthermore, while this is very early, your fix might also fix some general NET 4.7.1 ASP MVC problems, i.e. when someone *thought* they were not referencing netstandard libraries (but can one be sure?! with all of the nugets one might be netstandard). So the issue is, after this extremely discouraging failure, in a our big biz solution, I returned all local libs from netstandard back to full Net 4.7.1 libs. But even then I still was getting the same problem. But your solution fixed this as well, hard to believe my eyes. I hope what I am seeing is not a phantom! – Nicholas Petersen May 05 '18 at 07:05
  • To be clear for everyone, what you would have seen before in the main `web.config` was: ``. Change that to as shown, though make sure to keep the ` debug="true"` attribute, and maybe also the `targetFramework="4.7.1"` attribute, though without that attribute things were fine. But VS gave a debug time dialogue warning when missing the debug="true"` attribute. So use the XML segment as given in the answer but with first line with the additional `debug` attribute: `` – Nicholas Petersen May 05 '18 at 07:09
  • I spent several days trying to track down a solution to this problem. This was the answer. – C S Mar 13 '19 at 22:24