【发布时间】:2020-05-09 04:36:18
【问题描述】:
是否可以使用 Html.Raw 将字符串转换为超链接。这个代码是什么?
我正在尝试将 <a> 标签嵌入到 Razor 页面中,其中包含以下内容:
@{
string strText = "<title>Link Test</title><a class=\"nav-link text-dark\" target=\"_new\" asp-
area=\"Test\" asp-controller=\"Test\" asp-action=\"ViewFile\" asp-route-Id=1> View File Test</a>";
}
@Html.Raw(strText)
我的页面只显示“查看文件测试”,没有链接。
当我在浏览器中查看页面源代码时,我看到以下内容:
<title>Link Test</title><a class="nav-link text-dark" target="_new" asp-area="Test" asp-controller="Test" asp-action="ViewFile" asp-route-Id=1> View File Test</a>
当我复制以上内容并将其粘贴到我的剃须刀页面时,一切正常。
只是为了好玩,我还尝试了以下方法并得到了相同的结果:
@{
string strText = "<title>Link Test</title><a class=\"nav-link text-dark\" target=\"_new\" asp-
area=\"Test\" asp-controller=\"Test\" asp-action=\"ViewFile\" asp-route-Id=1> View File Test</a>";
}
<text>@strText</text>
下面的例子可以正常工作,但上面的例子不行。我想这可能与嵌入的引号有关
@{
string strText = "My Text <a href=\"http://www.google.com\">My link</a>.";
}
@Html.Raw(strText)
【问题讨论】:
标签: asp.net-core