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);
};
}
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
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?
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.
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...
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...
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:
Startup Filter:
Startup:
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...
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 classKind regards,
Dennis
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
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.
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?
Hi Bor,
Did you get this working?
I'm facing the same issue :-(
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:
And after that the functions that uses umbracocontext works fine.
Hope it solves your problem.
Kind regards, Bor
Setting the _variationContextAccessor.VariationContext was the solution.
Thanks for taking the time to respond!
is working on a reply...