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

Area Support - Generating non-area parent urls incorrectly includes the area value of the current page #69

Closed sethsteenken closed 3 years ago

sethsteenken commented 4 years ago

Generating a parent url for an action or controller that is not part of an Area while on a page that is part of an Area causes the parent urls to assume the Area of the current page. This is because IUrlHelper.Action will use the current page's area route value when generating the url even if null is supplied as the RouteValues parameter.

string GetUrl(IUrlHelper urlHelper) => urlHelper.Action(Action, Controller, RouteValues);

When RouteValues above is null (AreaName not specified on BreadcrumbAttribute), the route value area on the current page is supplied when generating the url.

Looks like node url generating logic needs to explicitly include an empty area in the route values for nodes that do not specify an area. Something like this on BreadcrumbNode constructor:

Current:

if (!string.IsNullOrWhiteSpace(areaName))
{
    RouteValues = new
    {
        area = areaName
    };
}

New:

RouteValues = new
{
    area = areaName?.Trim() ?? string.Empty
};

This way, for controllers and actions that are not under an Area, the route values will explicitly say { area = string.Empty } so the url helper will generate the url properly. What do you think? My preliminary testing checks out. I'd be glad to make a pull request for this, but wanted to run it by someone else. Thanks!

zHaytam commented 4 years ago

Hello, you are right, this problem is indeed present. Your solution seems to be good, a PR would be very welcomed! Thank you

julianyus commented 3 years ago

Hello, any idea when this bug will be fixed?