Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bo Jacobsen 292 posts 1259 karma points
    Aug 07, 2019 @ 14:03
    Bo Jacobsen
    0

    How to register RenderControllerFactory in v8

    Hi all.

    Using Umbraco 8.1.1 and i wanna know how to register RenderControllerFactory.

    public class CultureinfoControllerFactory : RenderControllerFactory
    {
        public override IController CreateController(RequestContext requestContext, string controllerName)
        {
            // Makeing sure nothing is null, and that the header we need exists.
            if (requestContext != null
                && requestContext.HttpContext != null
                && requestContext.HttpContext.Request != null
                && requestContext.HttpContext.Request.Headers != null
                && requestContext.HttpContext.Request.Headers["set-cultureinfo"] != null
                && !string.IsNullOrEmpty(requestContext.HttpContext.Request.Headers["set-cultureinfo"]))
            {
                // Createing a cultureInfo from the set-cultureinfo header value.
                var cultureInfo = CultureInfo.CreateSpecificCulture(requestContext.HttpContext.Request.Headers["set-cultureinfo"].ToString());
    
                System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
                System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;
            }
    
            return base.CreateController(requestContext, controllerName);
        }
    }
    

    In v7 i did this

    using System.Web.Mvc;
    using Umbraco.Core;
    
    namespace My.WebApplication.Namespace
    {
        public class FactoryApplicationEvent : ApplicationEventHandler
        {
            protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ControllerBuilder.Current.SetControllerFactory(typeof(CultureinfoControllerFactory));
            }
        }
    }
    

    But i guess i have to do it in an IUserComposer now, but how?

    public class InstallerComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.???  
        }
    }
    

    Maybe i should use IRenderMvcController but i dunno what to put inside the Index. https://our.umbraco.com/documentation/Reference/Routing/custom-controllers

    public class MyRenderMvcController : IRenderMvcController
    {
        public void Execute(RequestContext requestContext)
        {
            throw new NotImplementedException();
        }
    
        public ActionResult Index(ContentModel model)
        {
            throw new NotImplementedException();
        }
    }
    
  • Nik 1142 posts 4730 karma points MVP 2x c-trib
    Aug 07, 2019 @ 14:13
    Nik
    100

    Hi Bo,

    I think you should be able to do something like this:

    public class InstallerComposer : ComponentComposer<InstallerComponent>
    {
    }
    
    public class InstallerComponent : IComponent
    {
        public void Initialize()
        {
              ControllerBuilder.Current.SetControllerFactory(typeof(CultureinfoControllerFactory));  //I'm not sure about this bit of code here 
        }
    
        public void Terminate()
        {
        }
    }
    

    However if you code needs to be in the equivalent of ApplicationStarting it would be worth having a read of these 3 blog posts:

    https://www.zpqrtbnk.net/posts/composing-umbraco-v8-components/ https://www.zpqrtbnk.net/posts/composing-umbraco-v8-collections/ https://www.zpqrtbnk.net/posts/composing-umbraco-v8/

    Nik

  • Bo Jacobsen 292 posts 1259 karma points
    Aug 08, 2019 @ 06:43
    Bo Jacobsen
    0

    Thanks Nik.

    Your code snippet did the trick.

Please Sign in or register to post replies

Write your reply to:

Draft