Umbraco RenderController
# help-with-umbraco
u
I think since Umbraco 12 there is no "RenderController" anymore. Is there an alternative? Like my problem is that whenever the home page loads i want the controller to do something for example fetch some data from my database.
s
Yes there is.. here's my
BlogOverviewController
as an example:
Copy code
csharp
using Cultiv.Site.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Common.Controllers;
using Umbraco.Cms.Web.Common.PublishedModels;

namespace Cultiv.Controllers;

public class BlogOverviewController : RenderController
{
    private readonly IVariationContextAccessor _variationContextAccessor;
    private readonly ServiceContext _serviceContext;
        
    public BlogOverviewController(ILogger<BlogOverviewController> logger, 
        ICompositeViewEngine compositeViewEngine, 
        IUmbracoContextAccessor umbracoContextAccessor,
        IVariationContextAccessor variationContextAccessor, 
        ServiceContext context)
        : base(logger, compositeViewEngine, umbracoContextAccessor)
    {
        _variationContextAccessor = variationContextAccessor;
        _serviceContext = context;
    }
        
    [HttpGet]
    public IActionResult Index([FromQuery(Name = "page")] int page = 1)
    {
        var publishedValueFallback = new PublishedValueFallback(_serviceContext, _variationContextAccessor);
        var model = new BlogOverviewModel(CurrentPage, publishedValueFallback) { Page = page };
        var allBlogPosts = model.Children.ToList();
            
        var skip = 10 * model.Page - 10;
            
        model.TotalPages = Convert.ToInt32(Math.Ceiling(allBlogPosts.Count / 10.0));
        model.PreviousPage = model.Page - 1;
        model.NextPage = model.Page + 1;
        model.IsFirstPage = model.Page <= 1;
        model.IsLastPage = model.Page >= model.TotalPages;
            
        var selection = allBlogPosts.OrderByDescending(x => x.CreateDate)
            .Skip(skip)
            .Take(10)
            .Select(publishedContent => publishedContent as BlogPost)
            .ToList();

        model.PagedBlogPosts = selection;

        return CurrentTemplate(model);
    }
}
Note that my document type alias is
blogOverview
and the controller needs to be prefixed with that word for it to kick in.
u
For some reason this seem not to be working. I created a "HomeController" and one of my Components has an alias "Home". In the HomeController Index function i added a breakpoint. For unknown reasons it wont go through it.
m
Can you share your code and is the URL your hitting a home doctype or is home a block? (Asking as component to me is part of a page not the page itself)
u
This is my Code: public class HomeController : Umbraco.Cms.Web.Common.Controllers.RenderController { private readonly IVariationContextAccessor _variationContextAccessor; private readonly ServiceContext _serviceContext; public HomeController(ILogger logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor, IVariationContextAccessor variationContextAccessor, ServiceContext context) : base(logger, compositeViewEngine, umbracoContextAccessor) { _variationContextAccessor = variationContextAccessor; _serviceContext = context; } [HttpGet] public IActionResult Index() { ViewBag.Test = "Test"; return CurrentTemplate(CurrentPage); } public IActionResult Home() { return CurrentTemplate(CurrentPage); } public IActionResult HomePage() { return CurrentTemplate(CurrentPage); } }
@Matt Wise
m
public override IActionResult Index() you shouldnt need the Get attribute
If you have a template named Home or HomePage either of those should work as well
u
Oh thank you very much. It worked :).
Sorry for the disturbance, but is there an alternative to ViewBag for the RenderController? I've tried to use ViewBag and it doesnt work. Im guessing that there should be a way to pass data from the controller to the view, without having the add properties in the umbraco backoffice for the corresponding component. If so, can you help me with that please?
@Matt Wise
h
You should be able to use TempData["MyVariable"]
m
ViewData["VariableName"] also works