/// <summary> /// Рендеринг текста с зонами, объявленных в контенте в виде [[zone=имя_зоны]] /// </summary> /// <param name="helper"></param> /// <param name="componentHelper"></param> /// <param name="text"></param> /// <param name="arguments"></param> /// <returns></returns> public static async Task <IHtmlContent> RenderZonesInText(this IViewComponentHelper componentHelper, IHtmlHelper helper, string text, IDictionary <string, object> arguments = null) { if (!zonesInTextRegex.IsMatch(text)) { return(new HtmlString(text)); } var matches = zonesInTextRegex.Matches(text); //наполним словарь для замены зон в тексте на их содержимое var dictionary = new Dictionary <string, string>(); foreach (Match match in matches) { if (match.Groups.Count > 1) { var group = match.Groups[1]; if (group.Success) { var zoneName = group.Value; if (!string.IsNullOrEmpty(zoneName) && !dictionary.ContainsKey(match.Value)) { var widgetContent = await componentHelper.WidgetZone(helper, zoneName, arguments); using (var sw = new StringWriter()) { widgetContent.WriteTo(sw, HtmlEncoder.Default); dictionary[match.Value] = sw.ToString(); } } } } } //делаем замены var result = zonesInTextRegex.Replace(text, (match => { if (match.Groups.Count > 1) { var group = match.Groups[1]; if (group.Success) { return(dictionary[match.Value]); } } return(string.Empty); })); return(new HtmlString(result)); }