在 .NET 4.7.1 中的 ASP.NET MVC 5 razor 视图中引用 netstandard 2.0 类型 [英] Reference netstandard 2.0 types in ASP.NET MVC 5 razor views in .NET 4.7.1

查看:22
本文介绍了在 .NET 4.7.1 中的 ASP.NET MVC 5 razor 视图中引用 netstandard 2.0 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 4.7.1 应该解决我们在从完整框架中引用 netstandard 2.0 库时遇到的问题.尽管有一些持续和痛苦的 dll 冲突警告和相关问题,并且需要手动更新到 PackageReferences (

它在 .NET Framework 4.7.1 上运行,我希望它在 .NET Framework 4.7.2 上运行.

注意:IDE 中仍会出现红色波浪线,并显示有关缺少 Enum 类型的错误消息.但由于这不会影响功能(Intellisense 工作和代码运行),我希望现在可以接受.

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

<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!

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.

这篇关于在 .NET 4.7.1 中的 ASP.NET MVC 5 razor 视图中引用 netstandard 2.0 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆