简体   繁体   English

使用.net标准2.0将.net4.0旧库转换为多个目标会产生HtmlString引用错误

[英]Converting .net4.0 old library to multiple targets with .net standard 2.0 gives HtmlString reference error

I'm porting an old class library so it can multi target both .net4.0 and .net standard. 我正在移植一个旧的类库,因此它可以多目标.net4.0和.net标准。 I need to use the same library on a .net core project. 我需要在.net核心项目上使用相同的库。

The library relies heavily on System.Web.Mvc and I'm using the Strategy 1 described here: Upgrading ASP.NET Libraries to .NET Standard 该库很大程度上依赖于System.Web.Mvc ,我正在使用这里描述的策略1: 将ASP.NET库升级到.NET Standard

The errors I couldn't solve are: 我无法解决的错误是:

1 - Reference to type 'HtmlString claims it is defined in 'System.Web', but it could no be found 1 - 对'HtmlString类型的引用声称它是在'System.Web'中定义的,但是找不到它 在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?) 2 - 'MvcHtmlString'不包含'ToHtmlString'的定义,并且没有扩展方法'ToHtmlString'可以找到接受类型'MvcHtmlString'的第一个参数(你是否缺少using指令或汇编引用?) 在VS2017上看起来像这样:

I created a new .net standard class library project and copied the files over. 我创建了一个新的.net标准类库项目并将文件复制过来。 This is my csproj file: 这是我的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>

I tried adding this to netstandard2.0 ItemGroup but it didn't help: 我尝试将其添加到netstandard2.0 ItemGroup但它没有帮助:

<Reference Include="System.Web" />

What do I need to do to fix? 我需要做些什么来修复? I've read some about the subject, but it's still confusing and the links I found didn't help. 我已经阅读了一些关于这个主题的内容,但它仍然令人困惑,我找到的链接也无济于事。


I ended up using a new code base only for .NET Core . 我最终只为.NET Core使用了一个新的代码库。 My .csproj is like this: 我的.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>

As @NightOwl888 explained, I needed Microsoft.AspNetCore.Html.Abstractions so I could use HtmlString instead of MvcHtmlString . 正如@ NightOwl888解释的那样,我需要Microsoft.AspNetCore.Html.Abstractions所以我可以使用HtmlString而不是MvcHtmlString

The Microsoft.AspNetCore.Mvc.ViewFeatures is necessary so the SelectExtensions like DropDownListFor for example could return a type compatible with HtmlString . Microsoft.AspNetCore.Mvc.ViewFeatures是必需的,因此像DropDownListFor这样的SelectExtensions可以返回与HtmlString兼容的类型。

I also had to replace all HtmlHelper with IHtmlHelper and ModelMetaData with ModelExplorer . 我也不得不更换所有HtmlHelperIHtmlHelperModelMetaDataModelExplorer

ModelMetadata.FromLambdaExpression is now ExpressionMetadataProvider.FromLambdaExpression . ModelMetadata.FromLambdaExpression现在是ExpressionMetadataProvider.FromLambdaExpression

As per the 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 2.0中可用功能的任何功能,则兼容模式将起作用。

In place of MvcHtmlString on .NET Standard, you can use the Microsoft.AspNetCore.Html.HtmlString type as per this answer . 代替.NET Standard上的MvcHtmlString ,您可以根据此答案使用Microsoft.AspNetCore.Html.HtmlString类型。

Since the base class of MvcHtmlString is HtmlString in .NET 4.0, you should return HtmlString and it will work in both cases (although you need to import a different namespace for each). 由于基类的MvcHtmlStringHtmlString在.NET 4.0中,你应该返回HtmlString ,它会在两种情况下工作(尽管你需要导入不同的命名空间为每个)。

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

You need a reference to Microsoft.AspNetCore.Html.Abstractions in your netstandard2.0 targeted references to use that type. 您需要在netstandard2.0目标引用中引用Microsoft.AspNetCore.Html.Abstractions以使用该类型。

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

As for ToHtmlString() , I am confused what Microsoft intends to do about it . 对于ToHtmlString() ,我很困惑微软打算如何处理它 However, adding an extension method between #if NETSTANDARD2.0 and #endif blocks (or a file that is conditionally included for compile) is a pretty straghtforward thing to do in your library. 但是,在#if NETSTANDARD2.0#endif块之间添加扩展方法(或者有条件地包含在编译中的文件)在库中是一个非常直接的事情。

However, as John Skeet pointed out in the comments, it may not be the best idea to expose 2 different types through the same API like this. 但是,正如John Skeet在评论中指出的那样,通过相同的API公开2种不同的类型可能不是最好的主意。 You might be better off making a separate APIs for .NET Framework and .NET Core and put them in separate platform-targeted libraries that reference a .NET Standard library containing the shared business logic. 您可能最好为.NET Framework和.NET Core制作单独的API,并将它们放在单独的以平台为目标的库中,这些库引用包含共享业务逻辑的.NET Standard库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM