Check for Preview in .NET 8.0

Leif Anderson asked on February 9, 2024 15:57

I am migrating an app from .NET Framework 4.8 to .NET 8.0. There is a method we use to check if you're currently in preview mode: System.Web.HttpContext.Current.Kentico().Preview().Enabled. This comes from the documentation about setting up Previews.

However, in .NET 8.0, I'm not seeing this. Is this functionality supported?

Correct Answer

Juraj Ondrus answered on February 11, 2024 07:24

In .NET Core development model, please try using HttpContext.Kentico().Preview().Enabled instead.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Elmar Höfinghoff answered on February 20, 2024 10:16

Or just @Context.Kentico()... in Razor Views

1 votesVote for this answer Mark as a Correct answer

Leif Anderson answered on March 4, 2024 19:30 (last edited on March 4, 2024 19:33)

Thanks, everyone for your help - that works.

I ended up having something like this in order to get products that aren't published when preview is enabled.

private readonly IHttpContextAccessor _contextAccessor;
public HomeController(IHttpContextAccessor contextAccessor)
{   
    _contextAccessor = contextAccessor;
}

public DocumentQuery<Product> GetHomeProducts()
{
    var query = ProductsProvider.GetProducts()
        .OnSite("SiteName")
        .OrderBy("Title");

    if (_contextAccessor.HttpContext.Kentico().Preview().Enabled)
    {
        query = query.Published(false);
        return query;
    }

    return query;
}
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.