C# 包含Html片段的编码和字符串
我试图在一些文本中突出显示搜索结果。我编写了一个扩展方法:C# 包含Html片段的编码和字符串,c#,asp.net-mvc-3,html-encode,C#,Asp.net Mvc 3,Html Encode,我试图在一些文本中突出显示搜索结果。我编写了一个扩展方法: public static string Highlight(this HtmlHelper html, string input, string searchPhrase) { Regex.Replace(input, "\\b" + searchPhrase + "\\b", "<strong>" + searchPhrase + "&l
public static string Highlight(this HtmlHelper html, string input, string searchPhrase)
{
Regex.Replace(input,
"\\b" + searchPhrase + "\\b",
"<strong>" + searchPhrase + "</strong>",
RegexOptions.IgnoreCase);
}
公共静态字符串突出显示(此HtmlHelper html、字符串输入、字符串搜索短语)
{
Regex.Replace(输入,
“\\b”+搜索短语+“\\b”,
“”+搜索短语+“”,
RegexOptions.IgnoreCase);
}
但很明显,当这是从视图中编码的Html时,Html标记只是作为文本的一部分呈现
有更好的方法吗?或者,如果我的想法是好的,我如何使它工作
public static MvcHtmlString Highlight(this HtmlHelper html, string input, string searchPhrase)
{
var value = Regex.Replace(
input,
"\\b" + searchPhrase + "\\b",
"<strong>" + searchPhrase + "</strong>",
RegexOptions.IgnoreCase
);
return MvcHtmlString.Create(value);
}
@Html.Highlight("foo", "f")