How would you implement a breadcrumb helper in asp.net mvc?
-
09-06-2019 - |
Question
I know you could make a helper pretty easily given the data. So, if possible, please only submit answers that also include getting the data.
Solution
We are using an action filter for this.
...
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var controller = (Controller) filterContext.Controller;
Breadcrumb[] breadcrumbs = _breadcrumbManager.PushBreadcrumb(_breadcrumbLinkText);
controller.ViewData.Add(breadcrumbs);
}
before you mention it, I too have a distaste for service location in the filter attributes - but we are left with few options. IBreadcrumbManager looks like this:
public interface IBreadcrumbManager
{
Breadcrumb[] PushBreadcrumb(string linkText);
}
The implementation puts Breadcrumb objects into the Session. The Url is HttpContext.Current.Request.RawUrl
OTHER TIPS
@Chris: something like this:
<%
foreach (var item in ViewData.Get<Breadcrumb[]>())
{
%>
<a href="<%= Server.HtmlEncode(item.Url) %>"><%= item.LinkText %></a> »
<%
}
%>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow