Copied to clipboard

Flag this post as spam?

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


  • Bor 12 posts 125 karma points
    Mar 24, 2022 @ 09:41
    Bor
    0

    Custom Middleware with access to umbracocontext

    I have a problem accessing PublishedContent inside a custom Middleware. I dont know how to register it the right way so I can access the backoffice content...

    Middleware:

    public class TestMiddleware
    {
        private readonly RequestDelegate _next;
        public TestMiddleware(RequestDelegate next) { _next = next; }
    
        public async Task InvokeAsync(HttpContext context, IUmbracoContextFactory umbracoContextFactory )
        {
            using (var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext())
            {
                var contentCache = umbracoContextReference.UmbracoContext.Content;
    
                var root = contentCache.GetByXPath($"//root").FirstOrDefault();
    
                var r = root?.Name ?? "null";
            }
    
            await _next.Invoke(context);
        }
    }
    

    Startup Filter:

    public class TestStartupFilter : IStartupFilter
    {
        public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) => app =>
        {
            app.UseMiddleware<TestMiddleware>();
            next(app);
        };
    }
    

    Startup:

        public void ConfigureServices(IServiceCollection services)
        {
            //services.AddTransient<IStartupFilter, TestStartupFilter>();
            services.AddUmbraco(_env, _config)
                .AddBackOffice()
                .AddWebsite()
                .AddComposers()
                .Build();
            services.AddTransient<IStartupFilter, TestStartupFilter >();
            services.AddMvc().AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<ShopUserCommonView>());
        }
    

    When I try to get the content from lets say a View, I get the content, the problem is only when trying to do it from a Middleware.

    I get the instance of IUmbracoContextFactory but when I try to get the content out of "UmbracoContext.Content" (variable root) it is always null...

  • Dennis 75 posts 397 karma points MVP 2x
    Mar 24, 2022 @ 13:44
    Dennis
    0

    Hi Bor,

    It's not entirely clear what you're experiencing. Do you get an instance of the IUmbracoContextFactory, but you can't get any content out of it? Do you get exceptions?

    From what I can see, your code looks fine, though I've never seen the IStartupFilter interface before. I usually register my middlewares directly in the startup class

    Kind regards,
    Dennis

  • Bor 12 posts 125 karma points
    Mar 24, 2022 @ 14:11
    Bor
    0

    Yes you are right its not clear what are the results, I'm sorry.. Well the root variable comes out null. So yes I get the instance of IUmbracoContextFactory but when I try to get the content out of "UmbracoContext.Content" it is always null...

    The registration of Middleware via IStartupFilter instance is from this forum, I also tried registering it directly via Startup, but always the same result...

    Can you show me a couple examples of how you usually register a middleware?

    Kind regards, Bor

  • Dennis 75 posts 397 karma points MVP 2x
    Mar 24, 2022 @ 15:57
    Dennis
    0

    I've written a custom middleware for our URL Tracker package, soon to be prereleased on NuGet. Here are some pointers:

    The middleware uses the UmbracoContextAccessor instead of the UmbracoContextFactory, so it is explicitly inserted in the pipeline after umbraco middleware to make sure that an UmbracoContext is present.

  • Bor 12 posts 125 karma points
    Mar 24, 2022 @ 16:36
    Bor
    0

    I tried registering the Middleware the same way and tried injecting IUmbracoContextAccessor in both constructor and InvokeAsync header, but after calling TryGetUmbracoContext I always get null out...

    Any other idea?

  • Rasmus Østergård 19 posts 81 karma points c-trib
    May 05, 2022 @ 12:38
    Rasmus Østergård
    0

    Hi Bor,

    Did you get this working?

    I'm facing the same issue :-(

  • Bor 12 posts 125 karma points
    May 05, 2022 @ 12:58
    Bor
    101

    Hi Rasmus,

    yes it was a stupid program design from my side...

    The Middleware I was building was meant for displaying custom images, but on the custom image path Umbraco couldn't know which is your current umbracocontext.
    I think that the problem is when you accessing the media (example address: ../../something.jpg), Umbraco doesn't have a default language and even if we use EnsureUmbracoContext(), there will not be any context...

    My solution was:

    // _variationContextAccessor => IVariationContextAccessor
    // _umbracoContextFactory => IUmbracoContextFactory
    _variationContextAccessor.VariationContext ??= new VariationContext(_defaultCultureAccessor.DefaultCulture);
    _umbracoContextFactory.EnsureUmbracoContext();
    

    And after that the functions that uses umbracocontext works fine.

    Hope it solves your problem.

    Kind regards, Bor

  • Rasmus Østergård 19 posts 81 karma points c-trib
    May 09, 2022 @ 06:54
    Rasmus Østergård
    1

    Setting the _variationContextAccessor.VariationContext was the solution.

    Thanks for taking the time to respond!

Please Sign in or register to post replies

Write your reply to:

Draft