gpt4 book ai didi

c# - 使用 .net 标准 2.0 将 .net4.0 旧库转换为多个目标会出现 HtmlString 引用错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:48:44 25 4
gpt4 key购买 nike

我正在移植一个旧的类库,以便它可以同时针对 .net4.0 和 .net 标准。我需要在 .net 核心项目上使用相同的库。

该库严重依赖于 System.Web.Mvc,我正在使用此处描述的策略 ​​1: Upgrading ASP.NET Libraries to .NET Standard

我无法解决的错误是:

1 - Reference to type 'HtmlString claims it is defined in 'System.Web', but it could no be found It looks like this on VS2017:

2 - 'MvcHtmlString' does not contain a definition for 'ToHtmlString' and no extension method 'ToHtmlString' accepting a first argument of type 'MvcHtmlString' could be found (are you missing a using directive or an assembly reference?) It looks like this on VS2017:

我创建了一个新的 .net 标准类库项目并将文件复制过来。这是我的 csproj 文件:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net40</TargetFrameworks>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.AspNet.Mvc" Version="3.0.50813.1" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
<Reference Include="mscorlib" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Web" />
</ItemGroup>
</Project>

我尝试将它添加到 netstandard2.0 ItemGroup 但它没有帮助:

<Reference Include="System.Web" />

我需要做什么来修复?我已经阅读了一些有关该主题的内容,但它仍然令人困惑,而且我找到的链接也没有帮助。


我最终只为 .NET Core 使用了新的代码库。我的 .csproj 是这样的:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Html.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.DataAnnotations" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="1.1.0" />
</ItemGroup>
</Project>

正如@NightOwl888 所解释的,我需要Microsoft.AspNetCore.Html.Abstractions,所以我可以使用HtmlString 而不是MvcHtmlString

Microsoft.AspNetCore.Mvc.ViewFeatures 是必需的,因此 SelectExtensionsDropDownListFor 可以返回与 兼容的类型HtmlString.

我还必须将所有 HtmlHelper 替换为 IHtmlHelper 并将 ModelMetaData 替换为 ModelExplorer

ModelMetadata.FromLambdaExpression 现在是 ExpressionMetadataProvider.FromLambdaExpression

最佳答案

根据 article you linked :

Provided that the library does not invoke any functionality that is beyond what is available in .NET Standard 2.0, compatibility mode will work.

代替 .NET Standard 上的 MvcHtmlString,您可以根据 this answer 使用 Microsoft.AspNetCore.Html.HtmlString 类型.

由于 MvcHtmlString 的基类是 .NET 4.0 中的 HtmlString,您应该返回 HtmlString 并且它在两种情况下都有效(尽管您需要为每个导入不同的命名空间)。

#if NET40
using HtmlString = System.Web.HtmlString;
#else
using HtmlString = Microsoft.AspNetCore.Html.HtmlString;
#endif

您需要引用 Microsoft.AspNetCore.Html.Abstractions在您的 netstandard2.0 目标引用中使用该类型。

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net40</TargetFrameworks>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.AspNetCore.Html.Abstractions" Version="2.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
<Reference Include="mscorlib" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Web" />
</ItemGroup>
</Project>

至于ToHtmlString(),我搞不懂是什么Microsoft intends to do about it .但是,在 #if NETSTANDARD2.0#endif block (或有条件地包含用于编译的文件)之间添加扩展方法是一件非常简单的事情图书馆。

但是,正如 John Skeet 在评论中指出的那样,像这样通过相同的 API 公开 2 种不同的类型可能不是最好的主意。为 .NET Framework 和 .NET Core 创建单独的 API 并将它们放在单独的平台目标库中可能会更好,这些库引用包含共享业务逻辑的 .NET Standard 库。

关于c# - 使用 .net 标准 2.0 将 .net4.0 旧库转换为多个目标会出现 HtmlString 引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48642257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com