编码解码html< a>标签在mvc中 [英] Encode Decode html <a> tag in mvc

查看:122
本文介绍了编码解码html< a>标签在mvc中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 <$ c 

$ c>保护字符串MakeLink(字符串txt)
{
正则表达式regx = new Regex((http | https)://([a-zA-Z0-9\\〜\ \ \\ @#\\ \\ $ \\%\\ ^ \\&安培;!放; \\ * \\(\\ )_\\-\\ = \\ + \\\\\\ / \\ \\.\\:\\;? \\\'\\,] *)?,RegexOptions.IgnoreCase);

MatchCollection mactches = regx.Matches(txt);

foreach(匹配匹配)
{
txt = txt.Replace(match.Value,< a href ='+ match.Value +'> ;+ match.Value +< / a>);
}

return txt;
}

当我把标签显示为这样
lt; a href =' http://asd.com 'gt; http://asd.com lt; / a gt;[现在我删除了&否则它会在我的问题中创建链接。 ]

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



您可以在这里找到的示例: http://www.arrangeactassert.com/using-html-raw-in-asp-net-mvc-razor-views/


I have comment box when i type the comment and if comment have any link then i automatically convert to the link by following way.

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;
        }

when i put the tag will show like this " lt;a href='http://asd.com'gt;http://asd.com lt;/a gt; " [ right now i removed & otherwise it create link in my Question. ]

解决方案

Use @Html.Raw method to print output of this method. This method renders unencoded HTML. More on MSDN

The example you can find here: http://www.arrangeactassert.com/using-html-raw-in-asp-net-mvc-razor-views/

这篇关于编码解码html&lt; a&gt;标签在mvc中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆