编码解码html<;a>;mvc中的标签

编码解码html<;a>;mvc中的标签,html,asp.net-mvc,encoding,Html,Asp.net Mvc,Encoding,我有评论框,当我键入评论,如果评论有任何链接,然后我会自动转换为以下方式的链接 protected string MakeLink(string txt) { Regex regx = new Regex("(http|https)://([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.Igno

我有评论框,当我键入评论,如果评论有任何链接,然后我会自动转换为以下方式的链接

protected string MakeLink(string txt)
        {
            Regex regx = new Regex("(http|https)://([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);

            MatchCollection mactches = regx.Matches(txt);

            foreach (Match match in mactches)
            {
                txt = txt.Replace(match.Value, "<a href='" + match.Value + "'>" + match.Value + "</a>");
            }

            return txt;
        }
受保护的字符串MakeLink(字符串txt)
{
Regex regx=new Regex(“(http://https):/([a-zA-Z0-9\\\~\!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
MatchCollection mactches=regx.Matches(txt);
foreach(麦琪牌)
{
txt=txt.Replace(match.Value,“”);
}
返回txt;
}
当我放置标签时,标签会显示如下
“lt;a href=''gt;lt;/a gt;”[现在我删除了我问题中的链接,否则会创建链接。]

使用
@Html.Raw
方法打印此方法的输出。此方法呈现未编码的HTML。更多关于

您可以在这里找到的示例:

+1了解前面的答案。 也可以使用HtmlString类型

protected HtmlString MakeLink(string txt)
        {
            Regex regx = new Regex("(http|https)://([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);

            MatchCollection mactches = regx.Matches(txt);

            foreach (Match match in mactches)
            {
                txt = txt.Replace(match.Value, "<a href='" + match.Value + "'>" + match.Value + "</a>");
            }

            return new HtmlString(txt);
        }
受保护的HtmlString MakeLink(字符串txt)
{
Regex regx=new Regex(“(http://https):/([a-zA-Z0-9\\\~\!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
MatchCollection mactches=regx.Matches(txt);
foreach(麦琪牌)
{
txt=txt.Replace(match.Value,“”);
}
返回新的HtmlString(txt);
}