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

Async Actions Not Recognized with 'Async' Suffix #109

Open aAboodi opened 5 months ago

aAboodi commented 5 months ago

It appears that the SmartBreadcrumbs functionality does not recognize asynchronous actions that have an Asyncsuffix in their names. While the actions work as expected, the breadcrumb feature fails to display them when the Async suffix is present, even if the method is declared with the asynckeyword.

For instance:

Not Recognized: The breadcrumb does not show the action Details:

[Breadcrumb("Details")]
public async Task<IActionResult> DetailsAsync(int id, string ln = "")
{   // codes with await keywords   }

Recognized: The breadcrumb correctly displays the action Details:

[Breadcrumb("Details")]
public async Task<IActionResult> Details(int id, string ln = "") 
{   // codes with await keywords   }

A similar issue occurred when attempting to make the Index action (default) of a HomeController asynchronous by naming it IndexAsync(). Despite not adding any annotations, the exception was triggered, likely due to the presence of a [DefaultBreadcrumb("Home")]] annotation above the controller class. However, it's puzzling why IndexAsync() wasn't recognized as an Index action, considering the controller and view treated it as such.

The exception message received was:

SmartBreadcrumbs.SmartBreadcrumbsException: 'HomeController doesn't contain a valid action named Index.

The exception was triggered at program.cs in the following line:

builder.Services.AddBreadcrumbs(Assembly.GetExecutingAssembly(), options =>
{
    options.TagName = "";
    options.TagClasses = "";
    options.OlClasses = "breadcrumb justify-content-center mb-0";
    options.LiClasses = "breadcrumb-item";
    options.ActiveLiClasses = "breadcrumb-item active text-white";
});