Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional support for IHtmlContent titles #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/BreadcrumbOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public class BreadcrumbOptions
/// </summary>
public Type ResourceType { get; set; }

/// <summary>
/// Set to true if you want to allow IHtmlContent ViewData values to be written as raw HTML
/// </summary>
public bool AllowHtmlContent { get; set; }

#endregion Properties

public BreadcrumbOptions()
Expand Down
13 changes: 12 additions & 1 deletion src/BreadcrumbTagHelper.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down