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

Solved: Support for Razor Class Library (also how to use Razor Pages in Areas) #64

Closed carolinemilam closed 4 years ago

carolinemilam commented 4 years ago

Because reflection is used to parse all the breadcrumb attributes, if you use controllers or razor pages in Razor Class Library (outside your application's assembly), you need a way to pass its assembly to the SmartBreadcrumbs constructor.

Added additional methods to ServiceCollectionExtensions.cs:

public static void AddBreadcrumbs(this IServiceCollection services, Assembly[] assembly) { AddBreadcrumbs(services, assembly, new BreadcrumbOptions()); }

    public static void AddBreadcrumbs(this IServiceCollection services, Assembly[] assembly, Action<BreadcrumbOptions> optionsSetter)
    {
        var options = new BreadcrumbOptions();
        optionsSetter.Invoke(options);
        AddBreadcrumbs(services, assembly, options);
    }

    private static void AddBreadcrumbs(IServiceCollection services, Assembly[] assembly, BreadcrumbOptions options)
    {
        var bm = new BreadcrumbManager(options);
        foreach(Assembly a in assembly)
        {
            bm.Initialize(a);
        }

        services.AddSingleton(bm);

        services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
    }

Then, just created an assembly array in Startup.cs: Assembly[] assemblies = new Assembly[] { GetType().Assembly, typeof(MyApp.Models.BrandConfig).Assembly }; services.AddBreadcrumbs(assemblies, options => { options.TagName = "nav"; options.TagClasses = ""; options.OlClasses = "breadcrumb"; options.LiClasses = "breadcrumb-item"; options.ActiveLiClasses = "breadcrumb-item active"; options.SeparatorElement = "<li class=\"separator\"> > "; options.DontLookForDefaultNode = true; });

(MyApp.Models.BrandConfig is just a static class in the razor class library - you can pick any class in your library).

Also, had trouble getting this to work with Razor Pages nested in areas. Some of the posts indicate this does not work with areas, but was able to get it to work by specifying the AreaName in the attribute. Example used in scaffolding Core 3.1 Identity pages:

using SmartBreadcrumbs.Attributes;

namespace MyApp.Areas.Identity.Pages.Account.Manage { [Breadcrumb("Email", FromPage = typeof(IndexModel), AreaName = "Identity")] public partial class EmailModel : PageModel { ...

zHaytam commented 4 years ago

Hello, sorry for the delay. Can you create a Merge Request with these changes? Concerning the Areas, it doesn't work when 2 areas have an action with the same name, that's the issue.

zHaytam commented 4 years ago

Do you also need this option?

carolinemilam commented 4 years ago

Hi,

I am new to this forum - this is a test to see if I can reply to the email. Please confirm if message comes through.

Caroline

Sent from my iPhone

On Oct 9, 2020, at 4:35 AM, Haytam Zanid notifications@github.com wrote:

Do you also need this option?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

zHaytam commented 4 years ago

Yes it does, you can reply to GitHub issues using an email too.

On Fri, 9 Oct 2020 at 13:52, carolinemilam notifications@github.com wrote:

Hi,

I am new to this forum - this is a test to see if I can reply to the email. Please confirm if message comes through.

Caroline

Sent from my iPhone

On Oct 9, 2020, at 4:35 AM, Haytam Zanid notifications@github.com wrote:

Do you also need this option?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/zHaytam/SmartBreadcrumbs/issues/64#issuecomment-706162715, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIFCCVC6I4RGT4BZZOST7ALSJ4BSHANCNFSM4OPD4JMQ .

carolinemilam commented 4 years ago

Great!

So, I actually made further changes to fix bugs i introduced and then I realized the whole design requires a unique model class name so Area Razor Page indexes need to be named uniquely (e.g. MyNamespace.Areas.CodeManager.CodeManagerIndexModel vs MyNamespace.Areas.CodeManager.IndexMode. Since I’m at beginning of project, an acceptable solution, but you may want to address before incorporating my changes in case someone is adding this to existing solution.

I am away from office and will work on getting you source code with a change log next week.

Regards, Caroline

Sent from my iPhone

On Oct 9, 2020, at 8:55 AM, Haytam Zanid notifications@github.com wrote:

Yes it does, you can reply to GitHub issues using an email too.

On Fri, 9 Oct 2020 at 13:52, carolinemilam notifications@github.com wrote:

Hi,

I am new to this forum - this is a test to see if I can reply to the email. Please confirm if message comes through.

Caroline

Sent from my iPhone

On Oct 9, 2020, at 4:35 AM, Haytam Zanid notifications@github.com wrote:

Do you also need this option?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/zHaytam/SmartBreadcrumbs/issues/64#issuecomment-706162715, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIFCCVC6I4RGT4BZZOST7ALSJ4BSHANCNFSM4OPD4JMQ .

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

zHaytam commented 4 years ago

Doesn't the sample project already include a working example of areas working?