Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle breadcrumbs in mvc dotnetcore

I'm working on a website (MVC dotnetcore) that has many pages and flows as well. A user can approach a single page from many different ways. I'm getting stuck when user wants to move back and I don't have any track of its last URL. And I'm getting confused that how to handle this situation?

Should I use a global variable to keep track of last URL??

but in this case, I've to do a repeatable code on each screen, or may be for each get/ post call.

OR

Should I use kind of breadcrumbs structure?

Like if user move Admin from Index, there would be something like this

Index >> Admin

And for further

Index >> Admin >> ManageUsers 

And these would be links to their related pages. But I dont know how to handle this in mvc dotnetcore? Any Idea??

Which approach is good to follow? Any kind of help will be appreciated.

like image 850
Naila Akbar Avatar asked Jul 04 '17 20:07

Naila Akbar


1 Answers

I know the answer is a bit old, but I got the same problem when I was creating websites. I didn't want to repeat code in both projects and I wanted the breadcrumbs to handle multiple levels, especially levels that aren't in the same Controller. I ended up creating a little package to manage all of this: SmartBreadcrumbs (available in Nuget).

It's very easy to use, all you need to do is:

  1. In your services configuration, add services.UseBreadcrumbs(GetType().Assembly);
  2. Add [DefaultBreadcrumb("My home")] to your default node (usually the Home)
  3. Add [Breadcrumb("Contact me", FromAction = "Index" FromController = typeof(HomeController))] to your other levels (FromAction can be any combination of Controller.Action)

PS: The default node is ALWAYS present, even without specifying FromAction.
More informations on the github link or Blog post.

EDIT: Updated examples for version 2.0.0

like image 191
Haytam Avatar answered Nov 09 '22 00:11

Haytam