Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Carl Sjöholm 44 posts 327 karma points
    Jan 05, 2022 @ 07:28
    Carl Sjöholm
    0

    Hi.

    After reading this I get how to do custom routes. However, we want dynamic custom routes.

    https://our.umbraco.com/Documentation/Reference/Routing/custom-routes

    For example:

    /any-url/any-route/category-page/product-page

    So the code I sort of want to use is this:

    .Configure<UmbracoPipelineOptions>(options =>
            {
                options.AddFilter(new UmbracoPipelineFilter(nameof(ProductController))
                {
                    Endpoints = app => app.UseEndpoints(endpoints =>
                    {
                        var categoryPages = umbracoContext.Content
                            .GetByXPath($"//{CategoryPage.ModelTypeAlias}")
                            .ToList();
    
                        var allLanguages = _localizationService.GetAllLanguages().ToList();
    
                        foreach (var content in categoryPages)
                        {
                            foreach (var language in allLanguages)
                            {
                                var culture = language.CultureInfo.Name;
                                endpoints.MapControllerRoute(
                                    content.Name(_variationContextAccessor, culture),
                                    content.Url(culture) + "/{sku}",
                                    new { Controller = "Product", Action = "Index" });
                            }
                        }
                    })
                });
            });
    

    Obviously this doesn't work because of the 'Singleton' instance of a IUmbracoBuilder in a IComponent. And injecting stuff in a IComposer is not supported either.

    Does anyone have an idea?

  • Carl Sjöholm 44 posts 327 karma points
    Jan 05, 2022 @ 09:11
    Carl Sjöholm
    0

    Even when I do as below the problem still exists because of when stuff is registered i.e. Composers seems to run before the registring of IPublishedContentCache. And IUmbracoContextAccessor is not available yet.

    [Weight(int.MaxValue)]
    public class ProjectComposer : IComposer
    {
        public void Compose(IUmbracoBuilder builder)
        {
            var serviceProvider = builder.Services.BuildServiceProvider();
            var contentCache = serviceProvider.GetRequiredService<IPublishedContentCache>();
    
            var categoryPages = contentCache
                .GetByXPath($"//{CategoryPage.ModelTypeAlias}")
                .ToList();
    
            //builder.Services.Configure<UmbracoPipelineOptions>(options =>
            //{
            //    options.AddFilter(new UmbracoPipelineFilter(nameof(ProductController))
            //    {
            //        Endpoints = app => app.UseEndpoints(endpoints =>
            //        {
            //            foreach (var content in categoryPages)
            //            {
            //                endpoints.MapControllerRoute(
            //                    "ProductController_"+ content.Name(culture).StripWhitespace().StripAccents(),
            //                    content.Url(culture) + "/{sku}",
            //                    new { Controller = "Product", Action = "Index" });
            //            }
            //        })
            //    });
            //});
        }
    }
    
  • Carl Sjöholm 44 posts 327 karma points
    Jan 05, 2022 @ 10:04
    Carl Sjöholm
    0

    Seem to be what ever I do the 'MainDom' or 'UmbracoContext' is not available yet. No matter the order I put this in.

  • Carl Sjöholm 44 posts 327 karma points
    Jan 12, 2022 @ 12:13
    Carl Sjöholm
    100

    Managed to solve it with IContentFinder.

Please Sign in or register to post replies

Write your reply to:

Draft