编码解码html< a>标签在mvc中 [英] Encode Decode html <a> tag in 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\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", 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< a>标签在mvc中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!