zHaytam / SmartBreadcrumbs

A utility library for ASP.NET Core (both MVC and Razor Pages) websites to easily add and customize breadcrumbs.
https://blog.zhaytam.com/2018/06/24/asp-net-core-using-smartbreadcrumbs/
MIT License
161 stars 77 forks source link

How do I create a node that links to a route with several parameters #60

Closed shmulyeng closed 4 years ago

shmulyeng commented 4 years ago

If I use the RouteValues property, the values get added as query string parameters. Is it possible to have RouteValues added as part of the route? Basically, I have a route that takes 4 parameters. It looks something like this: http://url/locations/ny/nyc/sky/234 How would I get the breadcrumb to link to that?

zHaytam commented 4 years ago

SmartBreadcrumbs just uses IUrlHelper methods and passes RouteValues to them, so the problem is most likely not the library.

How do you define your RouteValues? are they named properties? What about your method/action?

shmulyeng commented 4 years ago

The RouteValues are working. But they're just being appended to the url as QueryString parameters. Looking through the source, I realized I really need a URL generated by UrlHelper.RouteUrl rather than urlHelper.Action. I would submit a PR with this feature. My question is if it makes sense to make a new class (i.e. MvcRouteBreadcrumbNode) that would take a route name in the constructor instead of controller and action or rather just make another constructor for MvcBreadcrumbNode. Depending on which parameters are filled in (controller and action or routename), the GetUrl method would either call urlHelper.Action or urlHelper.RouteUrl. I think having separate classes makes more sense.

shmulyeng commented 4 years ago

I just tested this in my project and it works. If this approach makes sense to you, I'll submit a PR.

zHaytam commented 4 years ago

Can it just be a parameter (property) in the Node? Would that work? I think that would make it easy to configure, but I still don't fully understand the solution, do you pass an actual route instead of action/controller?

I always thought UrlHelper.Action fills route values too.

shmulyeng commented 4 years ago

This is the signature of the action: [Route("locations/{state?}", Name = "locations-state")] public IActionResult State(string state)

And this is how I add this route to the breadcrumbs: var childNodeState = new MvcRouteBreadcrumbNode("locations-state", response.Response.ParentLocation.Name) { RouteValues = new {state=state} };

shmulyeng commented 4 years ago

To answer your question better, yes, I pass in the actual route name. The example I posted only takes one parameter. I can technically fake out the route by passing "location" as the controller and the value for state as the action. But once I have more parameters, there's no way to specify them all as part of the url. As I mentioned earlier, RouteValues don't get appended to the actual url but are added as QueryString parameters. Working with the actual route allows me to use a route name and pass in as many parameters as needed. The UrlExtensions.RouteUrl will automatically build the correct url.

zHaytam commented 4 years ago

Fixed by #40 A NuGet package will be available once another issue gets fixed, thank you!