From 4ebce3d34638f193b5ce837c43728493aa81516f Mon Sep 17 00:00:00 2001 From: theFra985 Date: Wed, 26 May 2021 15:25:08 +0200 Subject: [PATCH] Added optional support for IHtmlContent titles --- src/BreadcrumbOptions.cs | 5 +++++ src/BreadcrumbTagHelper.cs | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/BreadcrumbOptions.cs b/src/BreadcrumbOptions.cs index fb92337..30e7e34 100644 --- a/src/BreadcrumbOptions.cs +++ b/src/BreadcrumbOptions.cs @@ -89,6 +89,11 @@ public class BreadcrumbOptions /// public Type ResourceType { get; set; } + /// + /// Set to true if you want to allow IHtmlContent ViewData values to be written as raw HTML + /// + public bool AllowHtmlContent { get; set; } + #endregion Properties public BreadcrumbOptions() diff --git a/src/BreadcrumbTagHelper.cs b/src/BreadcrumbTagHelper.cs index dcfac39..e50531e 100644 --- a/src/BreadcrumbTagHelper.cs +++ b/src/BreadcrumbTagHelper.cs @@ -1,8 +1,10 @@ using System.Collections.Generic; +using System.IO; using System.Reflection; using System.Text; using System.Text.Encodings.Web; using System.Threading.Tasks; +using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Infrastructure; @@ -135,7 +137,16 @@ private string ExtractTitle(string title, bool encode = true) } string key = title.Substring(9); - title = ViewContext.ViewData.ContainsKey(key) ? ViewContext.ViewData[key].ToString() : $"{key} Not Found"; + if (ViewContext.ViewData.TryGetValue(key, out var v)) + if (BreadcrumbManager.Options.AllowHtmlContent && v is IHtmlContent content) + using (var writer = new StringWriter()) { + content.WriteTo(writer, _htmlEncoder); + return writer.ToString(); + } + else + title = v.ToString(); + else + title = $"{key} Not Found"; return encode ? _htmlEncoder.Encode(title) : title; }