Copied to clipboard

Flag this post as spam?

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


  • Nathan Woulfe 447 posts 1665 karma points MVP 5x hq c-trib
    May 20, 2021 @ 08:21
    Nathan Woulfe
    0

    Adding custom surface routes - how to?

    I'm trying to define a controller route in a component, but running into issues with services not being available:

    public class RoutingComponent : IComponent
        {
            private readonly IEndpointRouteBuilder _endpointBuilder;
    
            public RoutingComponent(IEndpointRouteBuilder endpointBuilder)
            {
                _endpointBuilder = endpointBuilder;
            }
    
            public void Initialize()
            {
                // throws
            }
    
            public void Terminate()
            {
            }
        }
    

    Stack is this:

    InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Routing.IEndpointRouteBuilder' while attempting to activate 'Plumber.Web.Components.RoutingComponent'.

    How should I be approaching this? I need to define a particular route to manage with a content finder...

  • Mathias Hove 11 posts 74 karma points
    May 20, 2021 @ 11:27
    Mathias Hove
    0

    Wouldn't utilizing a startup filter give you access to the IEndpointRouteBuilder ?

    Create a startupfilter, register it in a composer and access the UseEndpoints() method in IApplicationBuilder.

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-5.0#extend-startup-with-startup-filters

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    May 20, 2021 @ 13:10
    Andy Butland
    0

    Does this help? In my package migration I've added an extension method that you can see here.

    Which is called from Startup.cs:

        public void ConfigureServices(IServiceCollection services) =>
            services.AddUmbraco(_env, _config)
                .AddBackOffice()
                .AddWebsite()
                .AddComposers()
                .AddPersonalisationGroups(_config)
                .Build();
    
        public void Configure(IApplicationBuilder app)
        {
            app.UseUmbraco()
                .WithMiddleware(u =>
                {
                    u.WithBackOffice();
                    u.WithWebsite();
                })
                .WithEndpoints(u =>
                {
                    u.UseInstallerEndpoints();
                    u.UseBackOfficeEndpoints();
                    u.UseWebsiteEndpoints();
                    u.UsePersonalisationGroupsEndpoints();
                });
        }
    

    So not using a component, but seems like you may be able to do similar.

    Andy

  • Mathias Hove 11 posts 74 karma points
    May 20, 2021 @ 13:53
    Mathias Hove
    0

    Andy, we are achieving the same, except that a startupfilter is separate from the startup class, and called under startup :) Benefit is that you don't need to call it manually trough an extension method. Its a better experience, when no setup from the user is required.

    Though I'm not 100% sure how routing interacts up against Umbraco, so it's something that needs testing !:)

Please Sign in or register to post replies

Write your reply to:

Draft