- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在移植一个旧的类库,以便它可以同时针对 .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
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?)
我创建了一个新的 .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
是必需的,因此 SelectExtensions
像 DropDownListFor
可以返回与 兼容的类型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/
var $fragment = $(" "); $("#x").text($fragment.length); 看到这个 fiddle ...... http:/
ASP.NET MVC 的优势 或 ?为什么? 最佳答案 如果它返回 MvcHtmlString 则无所谓; <%:会知道它是预转义的。因此,我会使用 <%:这样审稿人就不需要考虑“这是转义了吗?
我试图在 WkWebView 中显示我之前下载并存储在字符串中的 html 页面。 这就是我设置 WkWebView 的方式: webView = WKWebView(frame: self.
我正在努力升级一些使用 jQuery 1.7.2 版的旧软件。由于添加了一些新插件并且需要更新版本的 jQuery,我已将其升级到版本 1.12.1。 然而,有大量使用 jQuery(htmlStri
appendTo 的 API 文档列出可以为目标选择一个 HTML 字符串的方法。 但是,这似乎没有用,因为该集合仍然包含原始元素,并且 HTML 字符串似乎没有添加到 DOM 中的任何地方,我也看不
我稍微玩了一下 jQuery,然后有了一个想法,所以我决定试一试。 $('table').append(''); 如你所想,这产生了: table -thead --tr ---th ---th --
本类(class):HtmlString html = $html; } /** * Get the HTML string. * * @return string
我正在查看 MvcHtmlString 及其基类 HtmlString 的源代码,它们似乎都是字符串的无用包装器。 我记得有一次我做过这个练习并看到了好处,但现在想不起来了。当时我花了很多时间研究 M
当我尝试从 HTML 生成 PDF 时,一些土耳其语字符如 ĞÜŞİÖÇ ğüşıöç PDF 中缺少这些字符,我看到一个空格代替了这些字符,但我想打印该字符。 我的代码是: public virt
HtmlString 与 MvcHtmlString 这两者之间有什么区别,或者什么时候更喜欢其中一个? 编辑: 与 HtmlString 相比,MvcHtmlString 更受青睐的是 MvcHtm
我使用Jquery保存在html_data变量HTML代码中。特别是 Jquery(document).ready(function(){ html_data = $('.row').htm
它是一个在 Visual Studio 2017 上运行的 .Net Core 2.0 应用程序我想显示在 View 上返回的 HTML 字符串。 我添加了Microsoft.AspNetCore.H
如果是 ajax 请求,我有一个方法可以将 javascript 添加到部分 View 的末尾。但我的问题是,当返回脚本时,脚本标签已被删除,我要执行的函数是用纯文本编写的。 if (httpCont
我有一个函数可以做到这一点: document.body.innerHTML += 'lorem ipsum'; 它工作正常,但它破坏了我对其他 div 的引用(我读到这是因为它再次解析正文)。是否有
我有一个从 sqlite 数据库读取数据的应用程序,使用此方法读取数据并将其包含在对象中....我用 NSLog 进行了检查 #import "ViewController1.h" #import "
只是我想在我的对象中有一个属性序列化为字符串而不是 HtmlString(它是)。 这里的原因是对象在 json 中被序列化,toString 函数返回的不是对象的字符串内容,而是“[object O
我有一个包装器,只要字段有值,它就会将标题添加到字段中。该字段实际上是一个字符串,它包含来自 tinymce 文本框的 HTML。 要求:当字段为空或只有空格时,标题不应显示。 问题:html 中的空
你好, 我正在显示一个 html 页面,其中包含一个带有一些输入文本字段和一个提交按钮的表单。 如果我使用 loadRequest 显示页面: 一切正常。 如果我使用 loadHTMLString:b
我创建了一个宏来处理表单错误,如下所示: {% macro render_field_with_errors(field) %} {{ field.labe
我发出了一个http请求并收到了一个htmlString,现在我想将其转换为Dom对象以查询其元素。谢谢你的帮助 最佳答案 您可以创建一个容器对象(我在这里使用了 div),然后将您的 html 字符
我是一名优秀的程序员,十分优秀!