相关疑难解决方法(0)

Web核心中的WebUtility.HtmlDecode替换

我需要在.NET Core(MVC6)中解码HTML字符.看起来.NET Core没有WebUtility.HtmlDecode函数,之前每个人都用它..NET Core中是否存在替代品?

.net c# asp.net-core-mvc asp.net-core

72
推荐指数
5
解决办法
4万
查看次数

在MVC中将字符串渲染为html

这里已经提出类似的问题,但答案并没有解决我的问题.

如何将字符串呈现为html?基本上,我需要将一个字符串传递给其中包含"a"标记定义的视图.我想要的是将它呈现为可点击的实际html链接.

查看模型(简化):

public class MyViewModel
{
    public string MyLink { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在控制器(简化)中:

public IActionResult Index()
{
    MyViewModel model = new MyViewModel();

    model.MyLink = "<a href="http://www.google.com">http://www.google.com</a>"

    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

在视图中(在第一行):

@model MyNamespace.ViewModel.MyViewModel
Run Code Online (Sandbox Code Playgroud)

然后在下面,这些html标记(数字后面的第1行)显示结果(第2行).但它们都不是您可以点击的实际链接.

1
@Model.MyLink
<a href="http://www.google.com">http://www.google.com</a>

2
<pre>@Html.Raw(@Model.MyLink)</pre>
<a href="http://www.google.com">http://www.google.com</a>

3
@Html.Encode(@Html.Raw(@Model.MyLink))
&amp;lt;a href=&amp;quot;http://www.google.com&amp;quot;&amp;gt;http://www.google.com&amp;lt;/a&amp;gt;

4 
@Html.Encode(@Model.MyLink)
result same as #3.
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.提前致谢.

html asp.net-mvc hyperlink

2
推荐指数
1
解决办法
6186
查看次数