使用razor将Html转换为纯文本

问题描述 投票:0回答:2
razor html-encode
2个回答
1
投票

您可以使用正则表达式删除标签:

string plainText = Regex.Replace(MyVariable, "<.*?>", string.Empty);

0
投票

在你的 file.cshtml 中你可以尝试使用 html.WriteRaw() 帮助器。 但在.net core中必须指定正确的mvn类命名空间。 我在 cstml 文件中编写了自己的辅助方法。我使用 WriteLiteral() 方法将所有标签(如 /֒ 或 /
)转换为纯文本。 Writelitaeral 将文本直接写入缓冲区,因此我的方法返回空字符串。

@functions {
public string RemoveFormat(string message)
{
    WriteLiteral(message);
    return "";
}
}
...
<p>
@RemoveFormat(@Model.data)
...
© www.soinside.com 2019 - 2024. All rights reserved.