UmbracoContext helper
The UmbracoContext is a helpful service provided on each request to the website.
The UmbracoContext is the simplified way to work with the current request on your website.
You can use UmbracoContext to access the content and media cache. Other useful properties are the original and cleaned URLs of the current request. You can also check if the current request is running in "preview" mode.
How to reference UmbracoContext
If you are using Views you can reference the UmbracoContext with the syntax: @UmbracoContext.
If you need an UmbracoContext in your own controllers, you need to inject an IUmbracoContextAccessor.
The following is an example of how to get access to the UmbracoContext in a controller:
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Web;
namespace MyProject.Controllers.Api;
[ApiController]
[Route("/umbraco/api/people")]
public class PeopleController : Controller
{
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly IPublishedContentQuery _publishedContentQuery;
public PeopleController(
IUmbracoContextAccessor umbracoContextAccessor,
IPublishedContentQuery publishedContentQuery)
{
_umbracoContextAccessor = umbracoContextAccessor;
_publishedContentQuery = publishedContentQuery;
}
[HttpGet("getall")]
public ActionResult<IEnumerable<string>> GetAll()
{
// Try to get the UmbracoContext
if (_umbracoContextAccessor.TryGetUmbracoContext(out IUmbracoContext? context) == false)
{
return Problem("Unable to get UmbracoContext");
}
// Check if we're in preview mode using UmbracoContext
if (context.InPreviewMode)
{
// In preview mode, you might want to show draft content or additional info
return Ok(new
{
message = "Preview mode active",
isPreview = true
});
}
if (context.Content == null)
{
return Problem("Content Cache is null");
}
// Use IPublishedContentQuery to get root content
var rootNodes = _publishedContentQuery.ContentAtRoot();
if (!rootNodes.Any())
{
return Problem("No content found at root");
}
// Find a specific parent node (for example, "People" section)
var peopleNode = rootNodes
.FirstOrDefault()?
.Children()
.FirstOrDefault(c => c.ContentType.Alias == "people");
if (peopleNode == null)
{
return Problem("People node not found");
}
// Get only the direct children of the People node
var personNodes = peopleNode.Children()
.Where(c => c.ContentType.Alias == "person")
.Select(p => p.Name);
// Return results with UmbracoContext information
return Ok(new
{
people = personNodes,
// Include UmbracoContext properties in the response
contextInfo = new
{
isPreview = context.InPreviewMode,
currentUrl = context.CleanedUmbracoUrl?.ToString()
}
});
}
}UmbracoContext is registered with a scoped lifetime. See the Microsoft documentation for more information. A service scope is created for each request, which means you can resolve an instance directly in a controller.
Last updated
Was this helpful?