if (UmbracoContext?.InPreviewMode ?? false)
{
// Some Code here
}
This is inside our BlockList component render(default.cshtml) so we can show errors for editors and not end users.
There are two things I can think of that could cause this.
Your umbracoContext has a lowercase “U” at the beginning (Though this should give a compile error)
The preview cookie or session data is not properly cleared.
Two is probably more likely and easy to test.
If you open a new private windows and test the page, without logging in to the backoffice, is the InPreviewMode still true?
If not then it’s some session data that is causing it, if it is still true, then I think we need a bit more context.
Is this inside a controller, service or af Razor page, is there any details in the HttpContext etc.
I already injected IUmbracoContextAccessor into a class and passed IUmbracoContext as umbracoContext.
The issue is that once an editor save and preview a page from the back office, UmbracoContext?.InPreviewmode will always return true even if the editor decide to browse the site from the same browser through a normal url.
This is because UmbracoContext?.InPreviewmode - will always return the current user is in a preview mode and browsing the site.
This will create a confusion for the editor because both normal url and save & preview windows will be showing preview content.
For editors to see a published content they will have to use a different browser or log out of the previours browser.
Agree with @Saeve unfortunately leaving preview mode is a training issue and it’s easy to miss even for seasoned editors.
I use the following syntax to check btw thank to @rickbutterfield from his Block Preview docs:
if (umbracoContext.UmbracoContext.InPreviewMode) {
// some code
}