Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you implement a breadcrumb helper in asp.net mvc?

Tags:

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.

like image 469
Dane O'Connor Avatar asked Sep 15 '08 19:09

Dane O'Connor


People also ask

How breadcrumbs are implemented in ASP NET MVC?

To get start first create an ASP.NET Core MVC project. I am using Visual Studio. Once it is done, install the nuget package SmartBreadcrumbs . Once it is done, we need to add the following code in the Program.

What are breadcrumbs on a website?

Breadcrumbs are a secondary navigation aid that helps users easily understand the relation between their location on a page (like a product page) and higher level pages (a category page, for instance).

What is true about partial view?

It helps us to reduce code duplication. In other word a partial view enables us to render a view within the parent view. The partial view is instantiated with its own copy of a ViewDataDictionary object which is available with the parent view so that partial view can access the data of the parent view.


1 Answers

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

like image 160
Matt Hinze Avatar answered Sep 29 '22 22:09

Matt Hinze