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

Fixed issue with considering breadcrumbs on failing pages #28

Closed
wants to merge 4 commits into from
Closed
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
Binary file modified .vs/MvcBreadCrumbs/v14/.suo
Binary file not shown.
Binary file modified MvcBreadCrumbs.v12.suo
Binary file not shown.
34 changes: 23 additions & 11 deletions MvcBreadCrumbs/BreadCrumb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ internal static IHierarchyProvider HierarchyProvider

public static void Add(string url, string label)
{
// get a key for the Url.
var key =
url
.ToLower()
.GetHashCode();

var current = new StateEntry().WithKey(key)
.WithUrl(url)
.WithLabel(label);
var state = StateManager.GetState(SessionProvider.SessionId);
state.Push(url, label);
}
Expand Down Expand Up @@ -145,21 +154,24 @@ public static string Display(string cssClassOverride = "breadcrumb")
return sb.ToString();

}
public static string DisplayRaw()
{

var state = StateManager.GetState(SessionProvider.SessionId);
public static string DisplayRaw(string crumbConcatenator = ">")
{
var state = StateManager.GetState(SessionProvider.SessionId);

if (state.Crumbs != null && !state.Crumbs.Any())
return "<!-- BreadCrumbs stack is empty -->";
if (state.Crumbs != null && !state.Crumbs.Any())
return "<!-- BreadCrumbs stack is empty -->";

// don't allow blank labels to propagate outside
state.Crumbs.ToList().ForEach(x => { x.Label = string.IsNullOrWhiteSpace(x.Label) ? x.Action : x.Label; });

return string.Join(" > ",
state.Crumbs.Select(x => "<a href=\"" + x.Url + "\">" + x.Label + "</a>").ToArray());

}
return string.Join(" " + crumbConcatenator + " ",
state.Crumbs.Select(x =>
{
if (IsCurrentPage(x.Key))
return x.Label;
else
return "<a href=\"" + x.Url + "\">" + x.Label + "</a>";
}).ToArray());
}

private static bool IsCurrentPage(int compareKey)
{
Expand Down
12 changes: 12 additions & 0 deletions MvcBreadCrumbs/BreadCrumbAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ private static IProvideBreadCrumbsSession SessionProvider
}
}

public override void OnResultExecuted(ResultExecutedContext filterContext)
{
if (filterContext.Exception != null)
{
//if we have an exception on the result (for ANY reason), let's make sure that we don't
//track this failing page on the breadcrumb
var state = StateManager.GetState(SessionProvider.SessionId);
state.OnErrorRemoveCrumb(filterContext);
}

base.OnResultExecuted(filterContext);
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{

Expand Down
27 changes: 24 additions & 3 deletions MvcBreadCrumbs/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,30 @@ private void Add(string url, string label, Type resourceType = null, ActionExecu
.WithLevel(levels)
.WithLabel(ResourceHelper.GetResourceLookup(resourceType, label));

Crumbs.Add(Current);
}
}
Crumbs.Add(Current);
}


/// <summary>
/// provides a way to remove a crumb from the crumbs list
/// </summary>
/// <param name="context"></param>
public void OnErrorRemoveCrumb(ResultExecutedContext context)
{
var key =
context.HttpContext.Request.Url.LocalPath
.ToLower()
.GetHashCode();

if (Crumbs.Any(x => x.Key == key))
{
var crumb = Crumbs.Single(x => x.Key == key);
Crumbs.Remove(crumb);
}
}


}

public class StateEntry
{
Expand Down