.NET 4.7.1中ASP.NET MVC 5剃刀视图中的参考netstandard 2.0类型 [英] Reference netstandard 2.0 types in ASP.NET MVC 5 razor views in .NET 4.7.1

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

问题描述

.NET 4.7.1可以解决我们在完整框架中引用 netstandard 2.0 库时遇到的问题。尽管存在一些持续且痛苦的dll冲突警告和相关问题,并且需要手动更新到PackageReferences(查看此精彩的扩展名)。尽管如此,虽然可以看到下面的注释1,但仍然可以使它工作。如果我可以说:不幸的是,至少可以说没有VStudio帮助或有关此问题和相关问题的大量指导,直到现在,人们还是必须找到这样的解决方案。在github上的反向渠道上提供帮助。理想情况下,应该已经将此问题本身发送给消息:ASP.NET MVC 5尚不支持剃须刀中的netstandard ...希望他们只是向我们发送消息,如果为true!可以节省无尽浪费的时间!但是是这样吗?还是有解决办法?

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

这是ASP.NET MVC 5项目(甚至是针对4.7.1的项目)的问题。尽管普通的.cs代码可以工作,包括在控制器中,但对于剃刀视图( .cshtml 文件)中的任何代码,这都是 not 正确的。从 netstandard 库获得的剃刀视图中引用的任何类型都将完全失败。

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.

要重现此问题并确保它不仅仅是我自己的代码,我重现了此通过在最新版本的VStudio 2017(甚至是预览版,即15.7.0预览版4.0)中创建一个全新的ASP.NET MVC 5项目(在github上),然后制作一个新的 netstandard 项目只有几个类型,因此我可以练习在MVC 5视图页面中引用这些类型。确实,它仍然会失败。例如,来自netstandard项目的这种简单类型:

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:


类型'Enum'在程序集中定义未引用。您必须添加对程序集 netstandard,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = cc7b13ffcd2ddd51的引用。 AspMvc5WebApp471

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

如该存储库中所述,我什至试图重新编译自己的Microsoft.CodeDom版本。 Providers.DotNetCompilerPlatform 在 web.config 中引用它,但这并不能解决问题。

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.

因此,很高兴收到ASP.NET团队或任何其他可能知道如何解决此问题的人的来信,该修复可能意味着什么。或者,如果ASP.NET MVC 5根本无法在 netstandard 下使用,那么很高兴知道这是否是消息,以及是否支持 netstandard 可能会在不久的将来用于ASP.NET MVC 5,或者是否在某个路线图上?也许是什么原因导致了此故障?当然,听到是否有可以立即应用的修复程序将是最快乐的,但是无论哪种方式,我们都需要知道,否则 netstandard 对于那些人基本上是没有用的我们当中不能仅仅在一天之内转储ASP.NET MVC 5(在现实世界中尽可能多的人...)的人非常感谢。

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.

(注1:当我说过一段时间,Net Framework 4.7.1可以解决许多难题时,我和其他许多人都非常期待它,但不幸的是,它引入了自己的无尽的dll地狱之类的问题,或参见此处或此处或此处。例如,请参见 System.Net上的讨论.Http (绑定重定向不只是删除所有冲突警告,他们通常会提出自己的非常痛苦的内容)。现在有人希望4.7.2可以解决所有这些问题,尽管它不能为我解决这些ASP.NET MVC 5问题。)

(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 !!),我发现在Razor编译时,您缺少对netstandard.dll的引用(这就是错误所在)。

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>

通过这一更改,我得以使两个IDE在cshtml中向我展示该模型的Intellisense文件和呈现的页面!

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!

< img src = https://i.stack.imgur.com/XAEYn.png alt =在此处输入图片描述>

这是在.NET Framework 4.7.1上运行,我希望它可以在.NET Framework 4.7.2上运行。

This is running on .NET Framework 4.7.1 and I expect this to work on .NET Framework 4.7.2. as well.

注意:IDE中仍然会有一个红色的花体,其中包含有关Enum类型的错误消息。但这不会影响功能(Intellisense可以正常运行,并且代码可以运行),我希望现在可以接受。

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剃刀视图中的参考netstandard 2.0类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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